
    Ed                         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d	S )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(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    ?lib/python3.11/site-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FF###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   s]    < 	Dqs88OZ 	DCFFaK 	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.domainsr%   )r   r%   s     r   	get_fieldzIntegerRing.get_fieldB   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)>
        r(   )r'   algebraic_field)r   r(   	extensions      r   r*   zIntegerRing.algebraic_fieldW   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_AlgebraicFieldt   s6    
 ; 	.::addffbf---	. 	.r   c                 l    |                      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$(3q661--...r   c                 D    t          |                                          S z3Convert ``ModularInteger(int)`` to GMPY's ``mpz``. r   to_intr1   s      r   from_FFzIntegerRing.from_FF       188::r   c                 D    t          |                                          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       1vvr   c                      t          |          S rD   rE   r1   s      r   from_ZZ_pythonzIntegerRing.from_ZZ_python   rG   r   c                 D    |j         dk    rt          |j                  S dS z1Convert Python's ``Fraction`` to GMPY's ``mpz``. r   Ndenominatorr   	numeratorr1   s      r   from_QQzIntegerRing.from_QQ   +    =A 	$q{###	$ 	$r   c                 D    |j         dk    rt          |j                  S dS rK   rL   r1   s      r   from_QQ_pythonzIntegerRing.from_QQ_python   rP   r   c                 *    |                                 S )z3Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. )r>   r1   s      r   from_FF_gmpyzIntegerRing.from_FF_gmpy   s    xxzz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 dS )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                 `    |                     |          \  }}|dk    rt          |          S dS )z,Convert mpmath's ``mpf`` to GMPY's ``mpz``. r   N)to_rationalr   )r2   r   r3   r!   qs        r   from_RealFieldzIntegerRing.from_RealField   s8    ~~a  16 	q66M	 	r   c                 *    |j         dk    r|j        S d S )Nr   )yxr1   s      r   from_GaussianIntegerRingz$IntegerRing.from_GaussianIntegerRing   s!    3!8 	3J	 	r   c                 L    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   s3    1++1a 	a7Na7Nr   c                 "    t          ||          S )z Compute GCD of ``a`` and ``b``. )r   r9   s      r   r   zIntegerRing.gcd       1ayy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    Aww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588D
%((C	cB "!NULFNO3 3 3$ $ $D D D  * 15 I I I I 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      s,   4 4 - - - - - - - -                F E E E E E ) ) ) ) ) ) 9 9 9 9 9 9 1 1 1 1 1 1 " " " " " " P P P P P$*L P P Pf []]r   