a
    rh8                     @   s   d Z ddl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mZ dd	lmZ dd
lmZ dddZeG dd deee	Ze ZdS )z,Implementation of :class:`RealField` class.     )
SYMPY_INTSMPQ)Float)Field)SimpleDomain)CharacteristicZero)CoercionFailed)public)	MPContext)to_rationalTc                 C   s  t | j\}}t|}t|}|r*||kr2||fS d\}}}}|| }	}
|	|
 }|||  }||krfq|||||  |f\}}}}|
|	||
   }	}
qH|| | }t||}t|||  |||  }t||}|r|s||fS t|| t|| kr|j|jfS |j|jfS d S )N)r      r   r   )_mpmath_to_rationalZ_mpf_intr   abs	numeratordenominator)sZ	max_denomlimitpqp0q0p1q1ndaq2knumberbound1bound2 r"   Y/var/www/html/assistant/venv/lib/python3.9/site-packages/sympy/polys/domains/realfield.pyr      s,    


r   c                   @   s.  e Zd ZdZdZd ZZdZdZdZ	dZ
dZdZedd Zedd	 Zed
d Zedd Zd?ddZe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/d0Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&dAd9d:Z'd;d< Z(d=d> Z)dS )B	RealFieldz(Real numbers up to the given precision. RRTF5   c                 C   s   | j | jkS N)	precision_default_precisionselfr"   r"   r#   has_default_precisionG   s    zRealField.has_default_precisionc                 C   s   | j jS r'   )_contextprecr*   r"   r"   r#   r(   K   s    zRealField.precisionc                 C   s   | j jS r'   )r-   dpsr*   r"   r"   r#   r/   O   s    zRealField.dpsc                 C   s   | j S r'   )
_tolerancer*   r"   r"   r#   	toleranceS   s    zRealField.toleranceNc                 C   s   t  }|d u r |d u r | j|_n(|d u r0||_n|d u r@||_ntd|| _|j| _| d| _	| d| _
td|j d d| _| j
| j | _d S )NzCannot set both prec and dpsr   r         c   )r
   r)   r.   r/   	TypeErrorr-   Zmpf_dtypedtypezeroonemax
_max_denomr0   )r+   r.   r/   Ztolcontextr"   r"   r#   __init__W   s    
zRealField.__init__c                 C   s   | j S r'   )r6   r*   r"   r"   r#   tpq   s    zRealField.tpc                 C   s   t |trt|}| |S r'   )
isinstancer   r   r6   )r+   argr"   r"   r#   r7   y   s    
zRealField.dtypec                 C   s   t |to| j|jkS r'   )r?   r$   r(   )r+   otherr"   r"   r#   __eq__   s    zRealField.__eq__c                 C   s   t | jj| j| jfS r'   )hash	__class____name__r6   r(   r*   r"   r"   r#   __hash__   s    zRealField.__hash__c                 C   s   t || jS )z%Convert ``element`` to SymPy number. )r   r/   )r+   elementr"   r"   r#   to_sympy   s    zRealField.to_sympyc                 C   s.   |j | jd}|jr| |S td| dS )z%Convert SymPy's number to ``dtype``. )r   zexpected real number, got %sN)evalfr/   Z	is_Numberr7   r   )r+   exprr   r"   r"   r#   
from_sympy   s    
zRealField.from_sympyc                 C   s
   |  |S r'   r7   r+   rG   baser"   r"   r#   from_ZZ   s    zRealField.from_ZZc                 C   s
   |  |S r'   rL   rM   r"   r"   r#   from_ZZ_python   s    zRealField.from_ZZ_pythonc                 C   s   |  t|S r'   )r7   r   rM   r"   r"   r#   from_ZZ_gmpy   s    zRealField.from_ZZ_gmpyc                 C   s   |  |jt|j S r'   r7   r   r   r   rM   r"   r"   r#   from_QQ   s    zRealField.from_QQc                 C   s   |  |jt|j S r'   rR   rM   r"   r"   r#   from_QQ_python   s    zRealField.from_QQ_pythonc                 C   s   |  t|jt|j S r'   )r7   r   r   r   rM   r"   r"   r#   from_QQ_gmpy   s    zRealField.from_QQ_gmpyc                 C   s   |  ||| jS r'   )rK   rH   rI   r/   rM   r"   r"   r#   from_AlgebraicField   s    zRealField.from_AlgebraicFieldc                 C   s
   |  |S r'   rL   rM   r"   r"   r#   from_RealField   s    zRealField.from_RealFieldc                 C   s   |j s| |jS d S r'   )imagr7   realrM   r"   r"   r#   from_ComplexField   s    zRealField.from_ComplexFieldc                 C   s   t || j|dS )z*Convert a real number to rational number. )r   )r   r;   )r+   rG   r   r"   r"   r#   r      s    zRealField.to_rationalc                 C   s   | S )z)Returns a ring associated with ``self``. r"   r*   r"   r"   r#   get_ring   s    zRealField.get_ringc                 C   s   ddl m} |S )z2Returns an exact domain associated with ``self``. r   )QQ)Zsympy.polys.domainsr\   )r+   r\   r"   r"   r#   	get_exact   s    zRealField.get_exactc                 C   s   | j S )z Returns GCD of ``a`` and ``b``. )r9   r+   r   br"   r"   r#   gcd   s    zRealField.gcdc                 C   s   || S )z Returns LCM of ``a`` and ``b``. r"   r^   r"   r"   r#   lcm   s    zRealField.lcmc                 C   s   | j |||S )z+Check if ``a`` and ``b`` are almost equal. )r-   almosteq)r+   r   r_   r1   r"   r"   r#   rb      s    zRealField.almosteqc                 C   s   |dkS )z8Returns ``True`` if ``a >= 0`` and ``False`` otherwise. r   r"   r+   r   r"   r"   r#   	is_square   s    zRealField.is_squarec                 C   s   |dkr|d S dS )zNon-negative square root for ``a >= 0`` and ``None`` otherwise.

        Explanation
        ===========
        The square root may be slightly inaccurate due to floating point
        rounding error.
        r   g      ?Nr"   rc   r"   r"   r#   exsqrt   s    zRealField.exsqrt)NNN)T)N)*rE   
__module____qualname____doc__repZis_RealFieldZis_RRZis_ExactZis_NumericalZis_PIDZhas_assoc_RingZhas_assoc_Fieldr)   propertyr,   r(   r/   r1   r=   r>   r7   rB   rF   rH   rK   rO   rP   rQ   rS   rT   rU   rV   rW   rZ   r   r[   r]   r`   ra   rb   rd   re   r"   r"   r"   r#   r$   6   sT   





	

r$   N)T)rh   Zsympy.external.gmpyr   r   Zsympy.core.numbersr   Zsympy.polys.domains.fieldr   Z sympy.polys.domains.simpledomainr   Z&sympy.polys.domains.characteristiczeror   Zsympy.polys.polyerrorsr   Zsympy.utilitiesr	   Zmpmathr
   Zmpmath.libmpr   r   r$   r%   r"   r"   r"   r#   <module>   s   
& &