o
     ô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   út/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sympy/polys/domains/rationalfield.pyÚ__init__-   s   zRationalField.__init__c                 C   s   t |tƒrdS tS )z0Returns ``True`` if two domains are equivalent. T)Ú
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   ó   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@   ó   zRationalField.to_sympyc                 C   sF   |j r
t|j|jƒS |jrddlm} ttt| 	|¡ƒŽ S t
d| ƒ‚)z&Convert SymPy's Integer to ``dtype``. r   )ÚRRz"expected `Rational` object, got %s)Úis_Rationalr   ÚpÚqÚ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} || g|¢R 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)Ú	is_groundÚconvertÚLCÚdom©ÚK1r"   ÚK0r   r   r   Úfrom_AlgebraicFieldl   s   ÿz!RationalField.from_AlgebraicFieldc                 C   ó   t |ƒS ©z.Convert a Python ``int`` object to ``dtype``. r   r5   r   r   r   Úfrom_ZZt   r   zRationalField.from_ZZc                 C   r9   r:   r   r5   r   r   r   Úfrom_ZZ_pythonx   r   zRationalField.from_ZZ_pythonc                 C   ó   t |j|jƒS ©z3Convert a Python ``Fraction`` object to ``dtype``. ©r   r   r    r5   r   r   r   Úfrom_QQ|   ó   zRationalField.from_QQc                 C   r=   r>   r?   r5   r   r   r   Úfrom_QQ_python€   rA   zRationalField.from_QQ_pythonc                 C   r9   )z,Convert a GMPY ``mpz`` object to ``dtype``. r   r5   r   r   r   Úfrom_ZZ_gmpy„   r   zRationalField.from_ZZ_gmpyc                 C   s   |S )z,Convert a GMPY ``mpq`` object to ``dtype``. r   r5   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   Úxr5   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+   r5   r   r   r   Úfrom_RealField‘   s   zRationalField.from_RealFieldc                 C   ó   t |ƒt |ƒ S )z=Exact quotient of ``a`` and ``b``, implies ``__truediv__``.  r   ©r   r"   Úbr   r   r   Úexquo•   ó   zRationalField.exquoc                 C   rI   )z6Quotient of ``a`` and ``b``, implies ``__truediv__``. r   rJ   r   r   r   Úquo™   rM   zRationalField.quoc                 C   s   | j S )z0Remainder of ``a`` and ``b``, implies nothing.  )ÚzerorJ   r   r   r   Úrem�   ó   zRationalField.remc                 C   s   t |ƒt |ƒ | jfS )z6Division of ``a`` and ``b``, implies ``__truediv__``. )r   rO   rJ   r   r   r   Údiv¡   r$   zRationalField.divc                 C   ó   |j S )zReturns numerator of ``a``. )r   r!   r   r   r   Únumer¥   rQ   zRationalField.numerc                 C   rS   )zReturns denominator of ``a``. )r    r!   r   r   r   Údenom©   rQ   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r!d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-   Úis_RationalFieldÚis_QQÚis_NumericalÚhas_assoc_RingÚhas_assoc_Fieldr   ÚdtyperO   ÚoneÚtypeÚtpr   r   r   r   r#   r,   r0   r8   r;   r<   r@   rB   rC   rD   rG   rH   rL   rN   rP   rR   rT   rU   r   rV   r   r   r   r   r      sH    

r   N)rZ   Úsympy.external.gmpyr   Úsympy.polys.domains.groundtypesr   r   r   Ú&sympy.polys.domains.characteristiczeror   Úsympy.polys.domains.fieldr   Ú sympy.polys.domains.simpledomainr	   Úsympy.polys.polyerrorsr
   Úsympy.utilitiesr   r   r   r   r   r   r   Ú<module>   s     
: