
    sg                         d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZ 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Ze G d deee             Z e       Zy
)z.Implementation of :class:`IntegerRing` class.     )MPZGROUND_TYPES)
int_valued)SymPyInteger	factorialgcdexgcdlcmsqrt	is_squaresqrtrem)CharacteristicZero)Ring)SimpleDomain)CoercionFailed)publicNc                      e Zd ZdZdZdZeZ ed      Z ed      Z	 e
e	      ZdxZZdZdZdZdZd Zd Zd Zd	 Zd
 Zd ZdddZd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d  Z*d! Z+d" Z,d# Z-y)$IntegerRinga  The domain ``ZZ`` representing the integers `\mathbb{Z}`.

    The :py:class:`IntegerRing` class represents the ring of integers as a
    :py:class:`~.Domain` in the domain system. :py:class:`IntegerRing` is a
    super class of :py:class:`PythonIntegerRing` and
    :py:class:`GMPYIntegerRing` one of which will be the implementation for
    :ref:`ZZ` depending on whether or not ``gmpy`` or ``gmpy2`` is installed.

    See also
    ========

    Domain
    ZZr      Tc                      y)z$Allow instantiation of this domain. N selfs    R/var/www/html/venv/lib/python3.12/site-packages/sympy/polys/domains/integerring.py__init__zIntegerRing.__init__3   s        c                 0    t        |t              ryt        S )z0Returns ``True`` if two domains are equivalent. T)
isinstancer   NotImplemented)r   others     r   __eq__zIntegerRing.__eq__6   s    e[)!!r   c                     t        d      S )z&Compute a hash value for this domain. r   )hashr   s    r   __hash__zIntegerRing.__hash__=   s    Dzr   c                 *    t        t        |            S )z!Convert ``a`` to a SymPy object. )r   intr   as     r   to_sympyzIntegerRing.to_sympyA   s    CF##r   c                     |j                   rt        |j                        S t        |      rt        t	        |            S t        d|z        )z&Convert SymPy's Integer to ``dtype``. zexpected an integer, got %s)
is_Integerr   pr   r'   r   r(   s     r   
from_sympyzIntegerRing.from_sympyE   s<    <<qss8O]s1v; !>!BCCr   c                     ddl m} |S )as  Return the associated field of fractions :ref:`QQ`

        Returns
        =======

        :ref:`QQ`:
            The associated field of fractions :ref:`QQ`, a
            :py:class:`~.Domain` representing the rational numbers
            `\mathbb{Q}`.

        Examples
        ========

        >>> from sympy import ZZ
        >>> ZZ.get_field()
        QQ
        r   )QQ)sympy.polys.domainsr0   )r   r0   s     r   	get_fieldzIntegerRing.get_fieldN   s    $ 	+	r   N)aliasc                B     | j                         j                  |d|iS )a  Returns an algebraic field, i.e. `\mathbb{Q}(\alpha, \ldots)`.

        Parameters
        ==========

        *extension : One or more :py:class:`~.Expr`.
            Generators of the extension. These should be expressions that are
            algebraic over `\mathbb{Q}`.

        alias : str, :py:class:`~.Symbol`, None, optional (default=None)
            If provided, this will be used as the alias symbol for the
            primitive element of the returned :py:class:`~.AlgebraicField`.

        Returns
        =======

        :py:class:`~.AlgebraicField`
            A :py:class:`~.Domain` representing the algebraic field extension.

        Examples
        ========

        >>> from sympy import ZZ, sqrt
        >>> ZZ.algebraic_field(sqrt(2))
        QQ<sqrt(2)>
        r3   )r2   algebraic_field)r   r3   	extensions      r   r5   zIntegerRing.algebraic_fieldc   s#    6 0t~~//H%HHr   c                 p    |j                   r*| j                  |j                         |j                        S y)zcConvert a :py:class:`~.ANP` object to :ref:`ZZ`.

        See :py:meth:`~.Domain.convert`.
        N)	is_groundconvertLCdomK1r)   K0s      r   from_AlgebraicFieldzIntegerRing.from_AlgebraicField   s+    
 ;;::addfbff-- r   c           	      p    | j                  t        t        j                  t        |      |                  S )a*  Logarithm of *a* to the base *b*.

        Parameters
        ==========

        a: number
        b: number

        Returns
        =======

        $\\lfloor\log(a, b)\\rfloor$:
            Floor of the logarithm of *a* to the base *b*

        Examples
        ========

        >>> from sympy import ZZ
        >>> ZZ.log(ZZ(8), ZZ(2))
        3
        >>> ZZ.log(ZZ(9), ZZ(2))
        3

        Notes
        =====

        This function uses ``math.log`` which is based on ``float`` so it will
        fail for large integer arguments.
        )dtyper'   mathlogr   r)   bs      r   rC   zIntegerRing.log   s'    < zz#dhhs1vq1233r   c                 6    t        |j                  |            S z3Convert ``ModularInteger(int)`` to GMPY's ``mpz``. r   to_intr<   s      r   from_FFzIntegerRing.from_FF       299Q<  r   c                 6    t        |j                  |            S rG   rH   r<   s      r   from_FF_pythonzIntegerRing.from_FF_python   rK   r   c                     t        |      S z,Convert Python's ``int`` to GMPY's ``mpz``. r   r<   s      r   from_ZZzIntegerRing.from_ZZ       1vr   c                     t        |      S rO   rP   r<   s      r   from_ZZ_pythonzIntegerRing.from_ZZ_python   rR   r   c                 L    |j                   dk(  rt        |j                        S yz1Convert Python's ``Fraction`` to GMPY's ``mpz``. r   Ndenominatorr   	numeratorr<   s      r   from_QQzIntegerRing.from_QQ   "    ==Aq{{## r   c                 L    |j                   dk(  rt        |j                        S yrV   rW   r<   s      r   from_QQ_pythonzIntegerRing.from_QQ_python   r[   r   c                 6    t        |j                  |            S )z3Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. rH   r<   s      r   from_FF_gmpyzIntegerRing.from_FF_gmpy   rK   r   c                     |S )z*Convert GMPY's ``mpz`` to GMPY's ``mpz``. r   r<   s      r   from_ZZ_gmpyzIntegerRing.from_ZZ_gmpy   s    r   c                 :    |j                   dk(  r|j                  S y)z(Convert GMPY ``mpq`` to GMPY's ``mpz``. r   N)rX   rY   r<   s      r   from_QQ_gmpyzIntegerRing.from_QQ_gmpy   s    ==A;; r   c                 ^    |j                  |      \  }}|dk(  rt        t        |            S y)z,Convert mpmath's ``mpf`` to GMPY's ``mpz``. r   N)to_rationalr   r'   )r=   r)   r>   r-   qs        r   from_RealFieldzIntegerRing.from_RealField   s0    ~~a 16 s1v;	 r   c                 :    |j                   dk(  r|j                  S y )Nr   )yxr<   s      r   from_GaussianIntegerRingz$IntegerRing.from_GaussianIntegerRing   s    33!833J r   c                 >    |j                   r| j                  |      S y)z*Convert ``Expression`` to GMPY's ``mpz``. N)r,   r.   r<   s      r   from_EXzIntegerRing.from_EX   s    <<==## r   c                 H    t        ||      \  }}}t        dk(  r|||fS |||fS )z)Compute extended GCD of ``a`` and ``b``. gmpy)r   r   )r   r)   rE   hsts         r   r   zIntegerRing.gcdex   s1    1+1a6!a7Na7Nr   c                     t        ||      S )z Compute GCD of ``a`` and ``b``. )r	   rD   s      r   r	   zIntegerRing.gcd       1ayr   c                     t        ||      S )z Compute LCM of ``a`` and ``b``. )r
   rD   s      r   r
   zIntegerRing.lcm   rt   r   c                     t        |      S )zCompute square root of ``a``. )r   r(   s     r   r   zIntegerRing.sqrt   s    Awr   c                     t        |      S )zReturn ``True`` if ``a`` is a square.

        Explanation
        ===========
        An integer is a square if and only if there exists an integer
        ``b`` such that ``b * b == a``.
        )r   r(   s     r   r   zIntegerRing.is_square   s     |r   c                 :    |dk  ryt        |      \  }}|dk7  ry|S )zuNon-negative square root of ``a`` if ``a`` is a square.

        See also
        ========
        is_square
        r   N)r   )r   r)   rootrems       r   exsqrtzIntegerRing.exsqrt  s*     q5AJ	c!8r   c                     t        |      S )zCompute factorial of ``a``. )r   r(   s     r   r   zIntegerRing.factorial  s    |r   ).__name__
__module____qualname____doc__repr3   r   rA   zeroonetypetpis_IntegerRingis_ZZis_Numericalis_PIDhas_assoc_Ringhas_assoc_Fieldr   r"   r%   r*   r.   r2   r5   r?   rC   rJ   rM   rQ   rT   rZ   r]   r_   ra   rc   rg   rk   rm   r   r	   r
   r   r   r{   r   r   r   r   r   r      s     CEE8D
(C	cB "!NULFNO3"$D* 15 I:.4@!!$
$
!
$
r   r   )r   sympy.external.gmpyr   r   sympy.core.numbersr   sympy.polys.domains.groundtypesr   r   r   r	   r
   r   r   r   &sympy.polys.domains.characteristiczeror   sympy.polys.domains.ringr    sympy.polys.domains.simpledomainr   sympy.polys.polyerrorsr   sympy.utilitiesr   rB   r   r   r   r   r   <module>r      s]    4 1 )   F ) 9 1 " |$*L | |~ ]r   