a
    RG5d  ã                   @   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   úZ/var/www/html/django/DPS/env/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   ©Ú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,   Ú_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_roundingÚ	_set_precÚ_set_dpsr   Úmpfr.   Ú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ÚdpsrA   Úrealr   r   r   Ú__init__/   s@    


zMPContext.__init__c                 C   s    d}dt d| j df}t||ƒS )N)r   é   é   é   r   r4   )r   rO   r   )rN   ZhundredÚepsr   r   r   r@   ^   s    zMPContext._make_tolc                 C   s   |   |  ¡ ¡S r   )rC   r@   ©rN   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-   r5   Ústrr   Ú
ValueError)rN   rA   rO   Úroundingr   r   r   rB   f   s    




zMPContext._convert_tolc                 C   s   t dt|ƒ ƒ‚d S )Nzcannot create mpf from )Ú	TypeErrorÚrepr)rN   ÚxÚstringsr   r   r   Ú_convert_fallbackr   s    zMPContext._convert_fallbackc                 C   s
   t | jƒS r   )r   Ú_precrW   r   r   r   Ú_repr_digitsu   s    zMPContext._repr_digitsc                 C   s   | j S r   )Ú_dpsrW   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   r4   r4   r   )r   r-   rE   r   ÚabsÚ_mpq_)rN   Ú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 rR|   |¡}t|| ƒ}||krjdS t|ƒ}t|ƒ}||k rŒ|| }n|| }||kS )NT)ÚconvertrD   rX   rh   )	rN   rj   ÚtÚrel_epsÚabs_epsÚdiffÚabssÚabstÚerrr   r   r   Úalmosteq›   s     


zMPContext.almosteq)r3   NNF)T)NN)r'   r(   r)   rR   r@   rX   rB   rc   r,   re   rg   r   r‚   r   r   r   r   r2   ,   s   
/


r2   N)!r*   Ú!sympy.polys.domains.domainelementr   Ú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__r:   r2   r   r   r   r   Ú<module>   s   @