a
    RG5dÄ  ã                   @   sš   d Z ddlmZmZ ddl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„ deeeƒƒZeƒ Zd	S )z.Implementation of :class:`IntegerRing` class. é    )ÚMPZÚHAS_GMPY)ÚSymPyIntegerÚ	factorialÚgcdexÚgcdÚlcmÚsqrt)ÚCharacteristicZero)ÚRing)ÚSimpleDomain)ÚCoercionFailed)ÚpublicNc                   @   s
  e Zd ZdZdZdZeZedƒZedƒZ	e
e	ƒZd ZZdZdZdZdZdd„ Zdd	„ Zd
d„ Zdd„ Zddœ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d$d%„ Z d&d'„ Z!d(d)„ Z"d*d+„ Z#d,d-„ Z$d.d/„ Z%d0d1„ Z&d2d3„ Z'd4d5„ Z(dS )6Ú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                 C   s   dS )z$Allow instantiation of this domain. N© )Úselfr   r   ú[/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/polys/domains/integerring.pyÚ__init__2   s    zIntegerRing.__init__c                 C   s   t t|ƒƒS )z!Convert ``a`` to a SymPy object. )r   Úint©r   Úar   r   r   Úto_sympy5   s    zIntegerRing.to_sympyc                 C   s>   |j rt|jƒS |jr.t|ƒ|kr.tt|ƒƒS td| ƒ‚dS )z&Convert SymPy's Integer to ``dtype``. zexpected an integer, got %sN)Ú
is_Integerr   ÚpÚis_Floatr   r   r   r   r   r   Ú
from_sympy9   s
    
zIntegerRing.from_sympyc                 C   s   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.domainsr   )r   r   r   r   r   Ú	get_fieldB   s    zIntegerRing.get_fieldN)Úaliasc                G   s   |   ¡ 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)>
        r!   )r    Úalgebraic_field)r   r!   Ú	extensionr   r   r   r"   W   s    zIntegerRing.algebraic_fieldc                 C   s   |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   ÚK0r   r   r   Úfrom_AlgebraicFieldt   s    zIntegerRing.from_AlgebraicFieldc                 C   s   |   t 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.
        )ÚdtypeÚmathÚlogr   ©r   r   Úbr   r   r   r.   |   s    zIntegerRing.logc                 C   s   t | ¡ ƒS ©z3Convert ``ModularInteger(int)`` to GMPY's ``mpz``. ©r   Úto_intr(   r   r   r   Úfrom_FFœ   s    zIntegerRing.from_FFc                 C   s   t | ¡ ƒS r1   r2   r(   r   r   r   Úfrom_FF_python    s    zIntegerRing.from_FF_pythonc                 C   s   t |ƒS ©z,Convert Python's ``int`` to GMPY's ``mpz``. ©r   r(   r   r   r   Úfrom_ZZ¤   s    zIntegerRing.from_ZZc                 C   s   t |ƒS r6   r7   r(   r   r   r   Úfrom_ZZ_python¨   s    zIntegerRing.from_ZZ_pythonc                 C   s   |j dkrt|jƒS dS ©z1Convert Python's ``Fraction`` to GMPY's ``mpz``. r   N©Údenominatorr   Ú	numeratorr(   r   r   r   Úfrom_QQ¬   s    
zIntegerRing.from_QQc                 C   s   |j dkrt|jƒS dS r:   r;   r(   r   r   r   Úfrom_QQ_python±   s    
zIntegerRing.from_QQ_pythonc                 C   s   |  ¡ S )z3Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. )r3   r(   r   r   r   Úfrom_FF_gmpy¶   s    zIntegerRing.from_FF_gmpyc                 C   s   |S )z*Convert GMPY's ``mpz`` to GMPY's ``mpz``. r   r(   r   r   r   Úfrom_ZZ_gmpyº   s    zIntegerRing.from_ZZ_gmpyc                 C   s   |j dkr|jS dS )z(Convert GMPY ``mpq`` to GMPY's ``mpz``. r   N)r<   r=   r(   r   r   r   Úfrom_QQ_gmpy¾   s    
zIntegerRing.from_QQ_gmpyc                 C   s"   |  |¡\}}|dkrt|ƒS dS )z,Convert mpmath's ``mpf`` to GMPY's ``mpz``. r   N)Úto_rationalr   )r)   r   r*   r   Úqr   r   r   Úfrom_RealFieldÃ   s    zIntegerRing.from_RealFieldc                 C   s   |j dkr|jS d S )Nr   )ÚyÚxr(   r   r   r   Úfrom_GaussianIntegerRingÊ   s    
z$IntegerRing.from_GaussianIntegerRingc                 C   s,   t ||ƒ\}}}tr|||fS |||fS dS )z)Compute extended GCD of ``a`` and ``b``. N)r   r   )r   r   r0   ÚhÚsÚtr   r   r   r   Î   s    
zIntegerRing.gcdexc                 C   s
   t ||ƒS )z Compute GCD of ``a`` and ``b``. )r   r/   r   r   r   r   Ö   s    zIntegerRing.gcdc                 C   s
   t ||ƒS )z Compute LCM of ``a`` and ``b``. )r   r/   r   r   r   r   Ú   s    zIntegerRing.lcmc                 C   s   t |ƒS )zCompute square root of ``a``. )r	   r   r   r   r   r	   Þ   s    zIntegerRing.sqrtc                 C   s   t |ƒS )zCompute factorial of ``a``. )r   r   r   r   r   r   â   s    zIntegerRing.factorial))Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrepr!   r   r,   ÚzeroÚoneÚtypeÚtpÚis_IntegerRingÚis_ZZÚis_NumericalÚis_PIDÚhas_assoc_RingÚhas_assoc_Fieldr   r   r   r    r"   r+   r.   r4   r5   r8   r9   r>   r?   r@   rA   rB   rE   rH   r   r   r   r	   r   r   r   r   r   r      sF   	 r   )rO   Úsympy.external.gmpyr   r   Úsympy.polys.domains.groundtypesr   r   r   r   r   r	   Z&sympy.polys.domains.characteristiczeror
   Úsympy.polys.domains.ringr   Ú sympy.polys.domains.simpledomainr   Úsympy.polys.polyerrorsr   Úsympy.utilitiesr   r-   r   r   r   r   r   r   Ú<module>   s     T