o
    *Î®c  ã                   @   sÊ   d 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 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. é    )Ú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_rational)Úmpqc                   @   ó2   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. ©Ú__mpf__c                 C   ó
   || _ d S ©Nr   ©ÚselfÚval© r    úE/tmp/pip-target-vg8gfxp4/lib/python/sympy/polys/domains/mpelements.pyÚ_set_mpf   ó   
zRealElement._set_mpfc                 C   ó   | j S r   r   ©r   r    r    r!   Ú<lambda>   ó    zRealElement.<lambda>c                 C   ó   | j jS r   ©ÚcontextÚ_parentr%   r    r    r!   Úparent   ó   zRealElement.parentN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r"   ÚpropertyÚ_mpf_r,   r    r    r    r!   r      ó    r   c                   @   r   )
ÚComplexElementz An element of a complex domain. ©Ú__mpc__c                 C   r   r   r7   r   r    r    r!   Ú_set_mpc"   r#   zComplexElement._set_mpcc                 C   r$   r   r7   r%   r    r    r!   r&   %   r'   zComplexElement.<lambda>c                 C   r(   r   r)   r%   r    r    r!   r,   '   r-   zComplexElement.parentN)	r.   r/   r0   r1   r2   r9   r3   Ú_mpc_r,   r    r    r    r!   r6      r5   r6   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 u r|  |¡ n|  |¡ t| _t| _| jt| jg| j_	| jt| jg| j_	|r3| | j_
n| | j_
t| _| jt| jg| j_	| | j_
| j| j| jg| _d| _d| _|d u r`|  ¡ | _n|du rht| _n|  |¡| _|  | j¡| _| js|d| _ntd| j ƒ| _|  t¡| _|  t¡| _|  ttf¡| _|  t¡| _|  t¡| _ |  t!¡| _"d S )NTFi@B é   )#r   Ú_prec_roundingÚ	_set_precÚ_set_dpsr   Úmpfr6   ÚmpcÚnewÚ_ctxdatar*   r   ÚconstantÚtypesÚtrap_complexÚprettyÚ	_make_tolÚtolr	   Ú_convert_tolÚmake_mpfÚ	toleranceÚ	max_denomÚintÚzeror
   ÚoneÚmake_mpcÚjr   Úinfr   Úninfr   Únan)ÚctxÚprecÚdpsrJ   Úrealr    r    r!   Ú__init__/   s@   


zMPContext.__init__c                 C   s    d}dt d| j df}t||ƒS )N)r   é   é   é   r   r=   )r   rX   r   )rW   ÚhundredÚepsr    r    r!   rI   ^   s   
zMPContext._make_tolc                 C   s   |   |  ¡ ¡S r   )rL   rI   ©rW   r    r    r!   Úmake_tolc   s   zMPContext.make_tolc                 C   s`   t |tƒr	t|ƒS t |tƒrt|ƒS t|dƒr|jS | j\}}t |tƒr*t	|||ƒS t
d| ƒ‚)Nr4   zexpected a real number, got %s)Ú
isinstancer   r   Úfloatr   Úhasattrr4   r>   Ústrr   Ú
ValueError)rW   rJ   rX   Úroundingr    r    r!   rK   f   s   




zMPContext._convert_tolc                 C   s   t dt|ƒ ƒ‚)Nzcannot create mpf from )Ú	TypeErrorÚrepr)rW   ÚxÚstringsr    r    r!   Ú_convert_fallbackr   s   zMPContext._convert_fallbackc                 C   s
   t | jƒS r   )r   Ú_precra   r    r    r!   Ú_repr_digitsu   s   
zMPContext._repr_digitsc                 C   r$   r   )Ú_dpsra   r    r    r!   Ú_str_digitsy   s   zMPContext._str_digitsTc                 C   sü   t |jƒ\}}|r|| jkr||fS d\}}}}||}	}
	 |	|
 }|||  }|| jkr.n|||||  |f\}}}}|
|	||
  }	}
q| j| | }t||ƒ}t|||  |||  ƒ}t||ƒ}|rh|sl||fS t|| ƒt|| ƒkr{|jS |jS )N)r   r=   r=   r   )r   r4   rN   r   ÚabsÚ_mpq_)rW   ÚsÚlimitÚpÚqÚp0Úq0Úp1Úq1ÚnÚdÚaÚq2ÚkÚnumberÚbound1Úbound2r    r    r!   r   }   s,   

ú

zMPContext.to_rationalc           	      C   s¢   |   |¡}|d u r|d u r| jp|  ¡  }}|d u r |   |¡}n	|d u r)|   |¡}t|| ƒ}||kr5dS t|ƒ}t|ƒ}||k rI|| }||kS || }||kS )NT)ÚconvertrM   rb   rr   )	rW   rt   ÚtÚrel_epsÚabs_epsÚdiffÚabssÚabstÚerrr    r    r!   Úalmosteq›   s"   

ÿzMPContext.almosteq)r<   NNF)T)NN)r.   r/   r0   r[   rI   rb   rK   rm   r3   ro   rq   r   rŒ   r    r    r    r!   r;   ,   s    
/


r;   N)!r1   Ú!sympy.polys.domains.domainelementr   Úsympy.utilitiesr   Úmpmath.ctx_mp_pythonr   r   r   r   Úmpmath.libmpr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Úmpmath.rationalr   r   r6   ÚobjectÚ__new__rC   r;   r    r    r    r!   Ú<module>   s    @