
    g                         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d
S )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-dS )$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                     dS )z$Allow instantiation of this domain. N selfs    [/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/polys/domains/integerring.py__init__zIntegerRing.__init__3   s          c                 >    t          |t                    rdS t          S )z0Returns ``True`` if two domains are equivalent. T)
isinstancer   NotImplemented)r   others     r   __eq__zIntegerRing.__eq__6   s    e[)) 	"4!!r   c                      t          d          S )z&Compute a hash value for this domain. r   )hashr   s    r   __hash__zIntegerRing.__hash__=   s    Dzzr   c                 :    t          t          |                    S )z!Convert ``a`` to a SymPy object. )r   intr   as     r   to_sympyzIntegerRing.to_sympyA   s    CFF###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   sO    < 	Dqs88O]] 	Ds1vv;; !>!BC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                @     |                                  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HHr   c                 n    |j         r-|                     |                                |j                  S dS )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   s6    
 ; 	.::addffbf---	. 	.r   c           	          |                      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   s0    < zz#dhs1vvq1122333r   c                 F    t          |                    |                    S z3Convert ``ModularInteger(int)`` to GMPY's ``mpz``. r   to_intr<   s      r   from_FFzIntegerRing.from_FF       299Q<<   r   c                 F    t          |                    |                    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       1vvr   c                      t          |          S rO   rP   r<   s      r   from_ZZ_pythonzIntegerRing.from_ZZ_python   rR   r   c                 D    |j         dk    rt          |j                  S dS z1Convert Python's ``Fraction`` to GMPY's ``mpz``. r   Ndenominatorr   	numeratorr<   s      r   from_QQzIntegerRing.from_QQ   '    =Aq{### r   c                 D    |j         dk    rt          |j                  S dS rV   rW   r<   s      r   from_QQ_pythonzIntegerRing.from_QQ_python   r[   r   c                 F    t          |                    |                    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 dS )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                 z    |                     |          \  }}|dk    rt          t          |                    S dS )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   s;    ~~a  166 s1vv;;	 6r   c                 *    |j         dk    r|j        S d S )Nr   )yxr<   s      r   from_GaussianIntegerRingz$IntegerRing.from_GaussianIntegerRing   s    3!883J 8r   c                 >    |j         r|                     |          S dS )z*Convert ``Expression`` to GMPY's ``mpz``. N)r,   r.   r<   s      r   from_EXzIntegerRing.from_EX   s(    < 	$==###	$ 	$r   c                 T    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   s6    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       1ayy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    Aww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                 J    |dk     rdS t          |          \  }}|dk    rdS |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  s4     q554AJJ	c!884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      s2         CEE588D
%((C	cB "!NULFNO3 3 3" " "  $ $ $D D D  * 15 I I I I I:. . .4 4 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      sR   4 4 1 1 1 1 1 1 1 1 ) ) ) ) ) )                    F E E E E E ) ) ) ) ) ) 9 9 9 9 9 9 1 1 1 1 1 1 " " " " " " | | | | |$*L | | |~ []]r   