
    MZd                         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eee             Z e       Zy	)z.Implementation of :class:`IntegerRing` class.     )MPZHAS_GMPY)SymPyInteger	factorialgcdexgcdlcmsqrt)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
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(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    A/usr/lib/python3/dist-packages/sympy/polys/domains/integerring.py__init__zIntegerRing.__init__2   s        c                 *    t        t        |            S )z!Convert ``a`` to a SymPy object. )r   intr   as     r   to_sympyzIntegerRing.to_sympy5   s    CF##r   c                     |j                   rt        |j                        S |j                  r"t	        |      |k(  rt        t	        |            S t        d|z        )z&Convert SymPy's Integer to ``dtype``. zexpected an integer, got %s)
is_Integerr   pis_Floatr   r   r   s     r   
from_sympyzIntegerRing.from_sympy9   sF    <<qss8OZZCFaK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.domainsr%   )r   r%   s     r   	get_fieldzIntegerRing.get_fieldB   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)>
        r(   )r'   algebraic_field)r   r(   	extensions      r   r*   zIntegerRing.algebraic_fieldW   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_AlgebraicFieldt   s+    
 ;;::addfbff-- r   c                 ^    | j                  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.
        )dtypemathlogr   r   r   bs      r   r8   zIntegerRing.log|   s"    < zz$((3q61-..r   c                 4    t        |j                               S z3Convert ``ModularInteger(int)`` to GMPY's ``mpz``. r   to_intr1   s      r   from_FFzIntegerRing.from_FF       188:r   c                 4    t        |j                               S r<   r=   r1   s      r   from_FF_pythonzIntegerRing.from_FF_python   r@   r   c                     t        |      S z,Convert Python's ``int`` to GMPY's ``mpz``. r   r1   s      r   from_ZZzIntegerRing.from_ZZ       1vr   c                     t        |      S rD   rE   r1   s      r   from_ZZ_pythonzIntegerRing.from_ZZ_python   rG   r   c                 L    |j                   dk(  rt        |j                        S yz1Convert Python's ``Fraction`` to GMPY's ``mpz``. r   Ndenominatorr   	numeratorr1   s      r   from_QQzIntegerRing.from_QQ   "    ==Aq{{## r   c                 L    |j                   dk(  rt        |j                        S yrK   rL   r1   s      r   from_QQ_pythonzIntegerRing.from_QQ_python   rP   r   c                 "    |j                         S )z3Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. )r>   r1   s      r   from_FF_gmpyzIntegerRing.from_FF_gmpy   s    xxzr   c                     |S )z*Convert GMPY's ``mpz`` to GMPY's ``mpz``. r   r1   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)rM   rN   r1   s      r   from_QQ_gmpyzIntegerRing.from_QQ_gmpy   s    ==A;; r   c                 L    |j                  |      \  }}|dk(  rt        |      S y)z,Convert mpmath's ``mpf`` to GMPY's ``mpz``. r   N)to_rationalr   )r2   r   r3   r!   qs        r   from_RealFieldzIntegerRing.from_RealField   s)    ~~a 16q6M r   c                 :    |j                   dk(  r|j                  S y )Nr   )yxr1   s      r   from_GaussianIntegerRingz$IntegerRing.from_GaussianIntegerRing   s    33!833J r   c                 B    t        ||      \  }}}t        r|||fS |||fS )z)Compute extended GCD of ``a`` and ``b``. )r   r   )r   r   r:   hsts         r   r   zIntegerRing.gcdex   s,    1+1aa7Na7Nr   c                     t        ||      S )z Compute GCD of ``a`` and ``b``. )r   r9   s      r   r   zIntegerRing.gcd       1ayr   c                     t        ||      S )z Compute LCM of ``a`` and ``b``. )r	   r9   s      r   r	   zIntegerRing.lcm   rf   r   c                     t        |      S )zCompute square root of ``a``. )r
   r   s     r   r
   zIntegerRing.sqrt   s    Awr   c                     t        |      S )zCompute factorial of ``a``. )r   r   s     r   r   zIntegerRing.factorial   s    |r   ))__name__
__module____qualname____doc__repr(   r   r6   zeroonetypetpis_IntegerRingis_ZZis_Numericalis_PIDhas_assoc_Ringhas_assoc_Fieldr   r   r#   r'   r*   r4   r8   r?   rB   rF   rI   rO   rR   rT   rV   rX   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:./@$
$

r   r   )rm   sympy.external.gmpyr   r   sympy.polys.domains.groundtypesr   r   r   r   r	   r
   &sympy.polys.domains.characteristiczeror   sympy.polys.domains.ringr    sympy.polys.domains.simpledomainr   sympy.polys.polyerrorsr   sympy.utilitiesr   r7   r   r   r   r   r   <module>r      sU    4 -  F ) 9 1 " P$*L P Pf ]r   