o
    Ðe  ã                   @   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. ©Z__mpf__c                 C   ó
   || _ d S ©Nr   ©ÚselfÚval© r   úND:\Projects\ConvertPro\env\Lib\site-packages\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. ©Z__mpc__c                 C   r   r   r6   r   r   r   r    Ú_set_mpc"   r"   zComplexElement._set_mpcc                 C   r#   r   r6   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.   r/   r0   r1   r7   r2   Z_mpc_r+   r   r   r   r    r5      r4   r5   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_roundingZ	_set_precZ_set_dpsr   Zmpfr5   ZmpcÚnewZ_ctxdatar)   r   ZconstantÚtypesZtrap_complexÚprettyÚ	_make_tolÚtolr	   Ú_convert_tolÚmake_mpfÚ	toleranceÚ	max_denomÚintÚzeror
   ÚoneZmake_mpcÚjr   Úinfr   Zninfr   Únan)ÚctxÚprecZdpsr@   Ú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   rL   r   )rK   ZhundredZepsr   r   r    r?   ^   s   
zMPContext._make_tolc                 C   s   |   |  ¡ ¡S r   )rB   r?   ©rK   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| ƒ‚)Nr3   zexpected a real number, got %s)Ú
isinstancer   r   Úfloatr   Úhasattrr3   r;   Ústrr   Ú
ValueError)rK   r@   rL   Úroundingr   r   r    rA   f   s   




zMPContext._convert_tolc                 C   s   t dt|ƒ ƒ‚)Nzcannot create mpf from )Ú	TypeErrorÚrepr)rK   ÚxÚstringsr   r   r    Ú_convert_fallbackr   s   zMPContext._convert_fallbackc                 C   s
   t | jƒS r   )r   Z_precrR   r   r   r    Ú_repr_digitsu   s   
zMPContext._repr_digitsc                 C   r#   r   )Z_dpsrR   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   r3   rD   r   ÚabsZ_mpq_)rK   Ú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)ÚconvertrC   rS   ra   )	rK   rb   ÚtZrel_epsZabs_epsÚdiffZabssZabstÚerrr   r   r    Úalmosteq›   s"   

ÿzMPContext.almosteq)r9   NNF)T)NN)r-   r.   r/   rN   r?   rS   rA   r^   r2   r_   r`   r   rv   r   r   r   r    r8   ,   s    
/


r8   N)!r0   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   Zmpmath.rationalr   r   r5   ÚobjectÚ__new__r<   r8   r   r   r   r    Ú<module>   s    @