a
    t@8b  ã                   @   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                   @   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   ú=lib/python3.9/site-packages/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   ©ÚcontextZ_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*   r/   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 u 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 u rÀ|  ¡ | _n|du 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_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dpsr7   Úrealr   r   r   Ú__init__/   s@    


zMPContext.__init__c                 C   s    d}dt d| j df}t||ƒS )N)r   é   é   é   r   r2   )r   rA   r   )r@   ZhundredZepsr   r   r   r6   ^   s    zMPContext._make_tolc                 C   s   |   |  ¡ ¡S r   )r9   r6   ©r@   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,   r3   Ústrr   Ú
ValueError)r@   r7   rA   Zroundingr   r   r   r8   f   s    




zMPContext._convert_tolc                 C   s   t dt|ƒ ƒ‚d S )Nzcannot create mpf from )Ú	TypeErrorÚrepr)r@   ÚxZstringsr   r   r   Ú_convert_fallbackr   s    zMPContext._convert_fallbackc                 C   s
   t | jƒS r   )r   Z_precrG   r   r   r   Ú_repr_digitsu   s    zMPContext._repr_digitsc                 C   s   | j S r   )Z_dpsrG   r   r   r   Ú_str_digitsy   s    zMPContext._str_digitsTc                 C   sþ   t |jƒ\}}|r|| jkr$||fS d\}}}}|| }	}
|	|
 }|||  }|| jkrZqŠ|||||  |f\}}}}|
|	||
   }	}
q:| j| | }t||ƒ}t|||  |||  ƒ}t||ƒ}|rÎ|sÖ||fS t|| ƒt|| ƒkrô|jS |jS d S )N)r   r2   r2   r   )r   r,   r;   r   ÚabsZ_mpq_)r@   ÚsÚlimitÚpÚqZp0Zq0Zp1Zq1ÚnÚdÚaZq2ÚkZnumberZbound1Z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 rR|   |¡}t|| ƒ}||krjdS t|ƒ}t|ƒ}||k rŒ|| }n|| }||kS )NT)Zconvertr:   rH   rT   )	r@   rU   ÚtZrel_epsZabs_epsZdiffZabssZabstÚerrr   r   r   Úalmosteq›   s     


zMPContext.almosteq)r1   NNF)T)NN)r&   r'   r(   rC   r6   rH   r8   rQ   r+   rR   rS   r   r_   r   r   r   r   r0   ,   s   
/


r0   N)!r)   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   r-   ÚobjectÚ__new__r4   r0   r   r   r   r   Ú<module>   s   @