B
    ["                 @   s   d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlm Z  e G dd	 d	eeZ!e G d
d de	eZ"e#j$Z%e G dd deZ&dS )zReal and complex elements.     )print_functiondivision)DomainElement)PythonMPContext_mpf_mpc	_constant)MPZ_ONEfzerofonefinffninffnanround_nearestmpf_mulmpf_absmpf_ltmpc_absrepr_dps	int_typesfrom_int
from_floatfrom_strto_rational)mpq)publicc               @   s4   e Zd ZdZdgZdd Zedd eZdd Zd	S )
RealElementzAn element of a real domain. __mpf__c             C   s
   || _ d S )N)r   )selfval r    =lib/python3.7/site-packages/sympy/polys/domains/mpelements.py_set_mpf   s    zRealElement._set_mpfc             C   s   | j S )N)r   )r   r    r    r!   <lambda>   s    zRealElement.<lambda>c             C   s   | j jS )N)context_parent)r   r    r    r!   parent   s    zRealElement.parentN)	__name__
__module____qualname____doc__	__slots__r"   property_mpf_r&   r    r    r    r!   r      s
   r   c               @   s4   e Zd ZdZdgZdd Zedd eZdd Zd	S )
ComplexElementz An element of a complex domain. __mpc__c             C   s
   || _ d S )N)r/   )r   r   r    r    r!   _set_mpc#   s    zComplexElement._set_mpcc             C   s   | j S )N)r/   )r   r    r    r!   r#   &   s    zComplexElement.<lambda>c             C   s   | j jS )N)r$   r%   )r   r    r    r!   r&   (   s    zComplexElement.parentN)	r'   r(   r)   r*   r+   r0   r,   Z_mpc_r&   r    r    r    r!   r.      s
   r.   c               @   sb   e Zd ZdddZdd Zdd Zd	d
 Zdd Zedd Z	edd Z
dddZdddZdS )	MPContext5   Nc             C   sR  |t g| _|d kr| | n
| | t| _t| _| jt| jg| j_	| jt| jg| j_	| | j_
| | j_
t| _| jt| jg| j_	| | j_
| j| j| jg| _d| _d| _|d kr|  | _n|dkrt| _n| || _| | j| _| jsd| _ntd| j | _| t| _| t| _| ttf| _| t| _| t| _ | t!| _"d S )NTFi@B    )#r   _prec_roundingZ	_set_precZ_set_dpsr   Zmpfr.   ZmpcnewZ_ctxdatar$   r   ZconstanttypesZtrap_complexZpretty	_make_toltolr
   _convert_tolmake_mpf	tolerance	max_denomintZzeror   ZoneZmake_mpcjr   infr   Zninfr   nan)ctxprecZdpsr8   r    r    r!   __init__0   s>    

zMPContext.__init__c             C   s    d}dt d| j df}t||S )N)r            r   r3   )r	   rB   r   )rA   ZhundredZepsr    r    r!   r7   [   s    zMPContext._make_tolc             C   s   |  |  S )N)r:   r7   )rA   r    r    r!   make_tol`   s    zMPContext.make_tolc             C   sd   t |trt|S t |tr$t|S t|dr4|jS | j\}}t |trTt	|||S t
d| d S )Nr-   zexpected a real number, got %s)
isinstancer   r   floatr   hasattrr-   r4   Z
basestringr   
ValueError)rA   r8   rB   Zroundingr    r    r!   r9   c   s    




zMPContext._convert_tolc             C   s   t dt| d S )Nzcannot create mpf from )	TypeErrorrepr)rA   xZstringsr    r    r!   _convert_fallbacko   s    zMPContext._convert_fallbackc             C   s
   t | jS )N)r   Z_prec)rA   r    r    r!   _repr_digitsr   s    zMPContext._repr_digitsc             C   s   | j S )N)Z_dps)rA   r    r    r!   _str_digitsv   s    zMPContext._str_digitsTc             C   s  t |j\}}|r|| jkr$||fS d\}}}}|| }	}
xR|	|
 }|||  }|| jkr\P |||||  |f\}}}}|
|	||
   }	}
q<W | j| | }t||}t|||  |||  }t||}|r|s||fS t|| t|| k r|jS |jS d S )N)r   r3   r3   r   )r   r-   r<   r   absZ_mpq_)rA   slimitpqZp0Zq0Zp1Zq1ndaZq2kZnumberZbound1Zbound2r    r    r!   r   z   s*    



zMPContext.to_rationalc       	      C   s   |  |}|d kr,|d kr,| jp&|   }}|d kr@|  |}n|d krR|  |}t|| }||krjdS t|}t|}||k r|| }n|| }||kS )NT)Zconvertr;   rG   rR   )	rA   rS   tZrel_epsZabs_epsZdiffZabssZabsterrr    r    r!   almosteq   s     


zMPContext.almosteq)r2   NN)T)NN)r'   r(   r)   rC   r7   rG   r9   rO   r,   rP   rQ   r   r]   r    r    r    r!   r1   -   s   
+
r1   N)'r*   Z
__future__r   r   Z!sympy.polys.domains.domainelementr   Zmpmath.ctx_mp_pythonr   r   r   r   Zmpmath.libmpr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zmpmath.rationalr   Zsympy.utilitiesr   r   r.   object__new__r5   r1   r    r    r    r!   <module>   s   L

