U
     ‹Ñh  ã                   @   sÊ   d Z ddlmZ ddl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 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. é    )ÚMPQ)ÚDomainElement)Úpublic)ÚPythonMPContextÚ_mpfÚ_mpcÚ	_constant)ÚMPZ_ONEÚfzeroÚfoneÚfinfÚfninfÚfnanÚround_nearestÚmpf_mulÚrepr_dpsÚ	int_typesÚfrom_intÚ
from_floatÚfrom_strÚto_rationalc                   @   s2   e Zd ZdZdZdd„ Zedd„ eƒZdd„ Zd	S )
ÚRealElementzAn element of a real domain. ©Z__mpf__c                 C   s
   || _ d S ©Nr   ©ÚselfÚval© r   úL/tmp/user/2202/pip-unpacked-wheel-qe3242ng/sympy/polys/domains/mpelements.pyÚ_set_mpf   s    zRealElement._set_mpfc                 C   s   | j S r   r   ©r   r   r   r   Ú<lambda>   ó    zRealElement.<lambda>c                 C   s   | j jS r   ©ÚcontextÚ_parentr    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                   @   s2   e Zd ZdZdZdd„ Zedd„ eƒZdd„ Zd	S )
ÚComplexElementz An element of a complex domain. ©Z__mpc__c                 C   s
   || _ d S r   r/   r   r   r   r   Ú_set_mpc"   s    zComplexElement._set_mpcc                 C   s   | j S r   r/   r    r   r   r   r!   %   r"   zComplexElement.<lambda>c                 C   s   | j jS 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 )Ú	MPContexté5   NFc                 C   sX  |t g| _|d kr|  |¡ n
|  |¡ t| _t| _| jt| jg| j_	| jt| jg| j_	|rf| | j_
n| | j_
t| _| jt| jg| j_	| | j_
| j| j| jg| _d| _d| _|d krÀ|  ¡ | _n|dkrÐt| _n|  |¡| _|  | j¡| _| jsød| _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_complexÚprettyÚ	_make_tolÚtolr
   Ú_convert_tolÚmake_mpfÚ	toleranceÚ	max_denomÚintZzeror   ZoneZmake_mpcÚjr   Úinfr   Zninfr   Únan)ÚctxÚprecZdpsr9   Úrealr   r   r   Ú__init__/   s@    


zMPContext.__init__c                 C   s    d}dt d| j df}t||ƒS )N)r   é   é   é   r   r3   )r	   rC   r   )rB   ZhundredZepsr   r   r   r8   ^   s    zMPContext._make_tolc                 C   s   |   |  ¡ ¡S r   )r;   r8   ©rB   r   r   r   Úmake_tolc   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   Ústrr   Ú
ValueError)rB   r9   rC   Úroundingr   r   r   r:   f   s    




zMPContext._convert_tolc                 C   s   t dt|ƒ ƒ‚d S )Nzcannot create mpf from )Ú	TypeErrorÚrepr)rB   ÚxÚstringsr   r   r   Ú_convert_fallbackr   s    zMPContext._convert_fallbackc                 C   s
   t | jƒS r   )r   Z_precrI   r   r   r   Ú_repr_digitsu   s    zMPContext._repr_digitsc                 C   s   | j S r   )Z_dpsrI   r   r   r   Ú_str_digitsy   s    zMPContext._str_digitsTc                 C   s  t |jƒ\}}t|ƒ}|r$|| jkr,||fS d\}}}}|| }	}
|	|
 }|||  }|| jkrbq’|||||  |f\}}}}|
|	||
   }	}
qB| j| | }t||ƒ}t|||  |||  ƒ}t||ƒ}|rÖ|sÞ||fS t|| ƒt|| ƒk�r|j|jfS |j|jfS d S )N)r   r3   r3   r   )r   r-   r>   r=   r   ÚabsÚ	numeratorÚdenominator)rB   ÚsÚlimitÚpÚqZp0Zq0Úp1Zq1ÚnÚdÚaZq2ÚkÚnumberZbound1Zbound2r   r   r   r   }   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)Úconvertr<   rJ   rX   )	rB   r[   ÚtZrel_epsZabs_epsZdiffZabssZabstÚerrr   r   r   Úalmosteq    s     


zMPContext.almosteq)r2   NNF)T)NN)r'   r(   r)   rE   r8   rJ   r:   rU   r,   rV   rW   r   rh   r   r   r   r   r1   ,   s   
/


#r1   N)!r*   Zsympy.external.gmpyr   Z!sympy.polys.domains.domainelementr   Zsympy.utilitiesr   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.   ÚobjectÚ__new__r5   r1   r   r   r   r   Ú<module>   s   @