U
    ZÊ¹j^  ã                   @   s‚   d Z ddlmZ ddl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 eG d	d
„ d
e
eeƒƒZeƒ ZdS )z0Implementation of :class:`RationalField` class. é    ©ÚMPQ)ÚSymPyRationalÚ	is_squareÚsqrtrem)ÚCharacteristicZero)ÚField)ÚSimpleDomain)ÚCoercionFailed)Úpublicc                   @   s  e Zd ZdZdZdZd ZZdZdZ	dZ
eZedƒZedƒZeeƒZdd„ Zd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$d0d1„ Z%d2d3„ Z&d4d5„ Z'd6d7„ Z(dS )8ÚRationalFieldaë  Abstract base class for the domain :ref:`QQ`.

    The :py:class:`RationalField` class represents the field of rational
    numbers $\mathbb{Q}$ as a :py:class:`~.Domain` in the domain system.
    :py:class:`RationalField` is a superclass of
    :py:class:`PythonRationalField` and :py:class:`GMPYRationalField` one of
    which will be the implementation for :ref:`QQ` depending on whether either
    of ``gmpy`` or ``gmpy2`` is installed or not.

    See also
    ========

    Domain
    ÚQQTr   é   c                 C   s   d S )N© ©Úselfr   r   ú`/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/polys/domains/rationalfield.pyÚ__init__-   s    zRationalField.__init__c                 C   s   t |tƒrdS tS dS )z0Returns ``True`` if two domains are equivalent. TN)Ú
isinstancer   ÚNotImplemented)r   Úotherr   r   r   Ú__eq__0   s    
zRationalField.__eq__c                 C   s   t dƒS )zReturns hash code of ``self``. r   )Úhashr   r   r   r   Ú__hash__7   s    zRationalField.__hash__c                 C   s   ddl m} |S )z'Returns ring associated with ``self``. r   )ÚZZ)Úsympy.polys.domainsr   )r   r   r   r   r   Úget_ring;   s    zRationalField.get_ringc                 C   s   t t|jƒt|jƒƒS )z!Convert ``a`` to a SymPy object. )r   ÚintÚ	numeratorÚdenominator©r   Úar   r   r   Úto_sympy@   s    zRationalField.to_sympyc                 C   sJ   |j rt|j|jƒS |jr:ddlm} ttt| 	|¡ƒŽ S t
d| ƒ‚dS )z&Convert SymPy's Integer to ``dtype``. r   )ÚRRz"expected `Rational` object, got %sN)Zis_Rationalr   ÚpÚqZis_Floatr   r#   Úmapr   Úto_rationalr
   )r   r!   r#   r   r   r   Ú
from_sympyD   s    zRationalField.from_sympyN)Úaliasc                G   s    ddl m} || f|ž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 QQ, sqrt
        >>> QQ.algebraic_field(sqrt(2))
        QQ<sqrt(2)>
        r   )ÚAlgebraicFieldr)   )r   r*   )r   r)   Ú	extensionr*   r   r   r   Úalgebraic_fieldN   s    zRationalField.algebraic_fieldc                 C   s   |j r|  | ¡ |j¡S dS )zbConvert a :py:class:`~.ANP` object to :ref:`QQ`.

        See :py:meth:`~.Domain.convert`
        N)Z	is_groundÚconvertZLCÚdom©ZK1r!   ZK0r   r   r   Úfrom_AlgebraicFieldl   s    z!RationalField.from_AlgebraicFieldc                 C   s   t |ƒS ©z.Convert a Python ``int`` object to ``dtype``. r   r/   r   r   r   Úfrom_ZZt   s    zRationalField.from_ZZc                 C   s   t |ƒS r1   r   r/   r   r   r   Úfrom_ZZ_pythonx   s    zRationalField.from_ZZ_pythonc                 C   s   t |j|jƒS ©z3Convert a Python ``Fraction`` object to ``dtype``. ©r   r   r   r/   r   r   r   Úfrom_QQ|   s    zRationalField.from_QQc                 C   s   t |j|jƒS r4   r5   r/   r   r   r   Úfrom_QQ_python€   s    zRationalField.from_QQ_pythonc                 C   s   t |ƒS )z,Convert a GMPY ``mpz`` object to ``dtype``. r   r/   r   r   r   Úfrom_ZZ_gmpy„   s    zRationalField.from_ZZ_gmpyc                 C   s   |S )z,Convert a GMPY ``mpq`` object to ``dtype``. r   r/   r   r   r   Úfrom_QQ_gmpyˆ   s    zRationalField.from_QQ_gmpyc                 C   s   |j dkrt|jƒS dS )z3Convert a ``GaussianElement`` object to ``dtype``. r   N)Úyr   Úxr/   r   r   r   Úfrom_GaussianRationalFieldŒ   s    
z(RationalField.from_GaussianRationalFieldc                 C   s   t tt| |¡ƒŽ S )z.Convert a mpmath ``mpf`` object to ``dtype``. )r   r&   r   r'   r/   r   r   r   Úfrom_RealField‘   s    zRationalField.from_RealFieldc                 C   s   t |ƒt |ƒ S )z=Exact quotient of ``a`` and ``b``, implies ``__truediv__``.  r   ©r   r!   Úbr   r   r   Úexquo•   s    zRationalField.exquoc                 C   s   t |ƒt |ƒ S )z6Quotient of ``a`` and ``b``, implies ``__truediv__``. r   r>   r   r   r   Úquo™   s    zRationalField.quoc                 C   s   | j S )z0Remainder of ``a`` and ``b``, implies nothing.  )Úzeror>   r   r   r   Úrem�   s    zRationalField.remc                 C   s   t |ƒt |ƒ | jfS )z6Division of ``a`` and ``b``, implies ``__truediv__``. )r   rB   r>   r   r   r   Údiv¡   s    zRationalField.divc                 C   s   |j S )zReturns numerator of ``a``. )r   r    r   r   r   Únumer¥   s    zRationalField.numerc                 C   s   |j S )zReturns denominator of ``a``. )r   r    r   r   r   Údenom©   s    zRationalField.denomc                 C   s   t |jƒot |jƒS )zÓReturn ``True`` if ``a`` is a square.

        Explanation
        ===========
        A rational number is a square if and only if there exists
        a rational number ``b`` such that ``b * b == a``.
        )r   r   r   r    r   r   r   r   ­   s    zRationalField.is_squarec                 C   sL   |j dk rdS t|j ƒ\}}|dkr(dS t|jƒ\}}|dkrBdS t||ƒS )zuNon-negative square root of ``a`` if ``a`` is a square.

        See also
        ========
        is_square
        r   N)r   r   r   r   )r   r!   Zp_sqrtZp_remZq_sqrtZq_remr   r   r   Úexsqrt·   s    
zRationalField.exsqrt))Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrepr)   Zis_RationalFieldZis_QQZis_NumericalZhas_assoc_RingZhas_assoc_Fieldr   ZdtyperB   ÚoneÚtypeÚtpr   r   r   r   r"   r(   r,   r0   r2   r3   r6   r7   r8   r9   r<   r=   r@   rA   rC   rD   rE   rF   r   rG   r   r   r   r   r      sF   

r   N)rK   Zsympy.external.gmpyr   Zsympy.polys.domains.groundtypesr   r   r   Z&sympy.polys.domains.characteristiczeror   Zsympy.polys.domains.fieldr   Z sympy.polys.domains.simpledomainr	   Zsympy.polys.polyerrorsr
   Zsympy.utilitiesr   r   r   r   r   r   r   Ú<module>   s    :