o
    "`;C                     @   s.  d dl Z ddlmZ ddl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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 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-m.Z.m/Z/ ddl0m1Z1 eefZ2ddl3m4Z4 e5j6Z7dd	 Z8G d
d de5Z9G dd de5Z:dd Z;e;ee%\e9_<e9_=e:_<e:_=e;ee&\e9_>e9_?e:_>e:_?e;ee'\e9_@e9_Ae:_@e:_Ae;ee(\e9_Be9_Ce:_Be:_Ce;e!e)\e9_De9_Ee:_De:_Ee9jBe9_Fe9jCe9_Ge:jBe:_Fe:jCe:_GG dd de9ZHG dd de4ZIzd dlJZJeJjKLe: eJjMLe9 W dS  eNy   Y dS w )    N   )libmp)
basestring),	int_typesMPZ_ONEprec_to_dpsdps_to_precrepr_dpsround_floorround_ceilingfzerofinffninffnanmpf_lempf_negfrom_int
from_floatfrom_strfrom_rationalmpi_mid	mpi_deltampi_strmpi_absmpi_posmpi_negmpi_addmpi_submpi_mulmpi_divmpi_pow_intmpi_powmpi_from_strmpci_posmpci_negmpci_addmpci_submpci_mulmpci_divmpci_powmpci_absr)   mpci_expmpci_logComplexResultmpf_hashmpc_hash)_matrix)StandardBaseContextc                 C   sV   t | dr| jS t| trt| ||S t| trt| ||S t| tr)t| ||S t	)N_mpf_)
hasattrr2   
isinstancer   r   floatr   r   r   NotImplementedError)xprecZrounding r9   //usr/lib/python3/dist-packages/mpmath/ctx_iv.pyconvert_mpf_   s
   r;   c                   @   s(  e Zd ZdZd=ddZ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 Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Zdd  ZeZeZeZeZd!d" Zd#d$ Zd%d& Zd'd  Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd>d;d<Z d:S )?ivmpfzH
    Interval arithmetic class. Precision is controlled by iv.prec.
    r   c                 C   s   | j |S N)ctxconvert)clsr7   r9   r9   r:   __new__+      zivmpf.__new__c                 C   s"   | j \}}||kr|||S tr=   )_mpi_
ValueError)selfr@   Z	f_convertabr9   r9   r:   cast.   s   
z
ivmpf.castc                 C      |  ttjS r=   )rH   intr   Zto_intrE   r9   r9   r:   __int__4      zivmpf.__int__c                 C   rI   r=   )rH   r5   r   to_floatrK   r9   r9   r:   	__float__7   rM   zivmpf.__float__c                 C   rI   r=   )rH   complexr   rN   rK   r9   r9   r:   __complex__:   rM   zivmpf.__complex__c                 C   s$   | j \}}||krt|S t| j S r=   )rC   r.   hashrE   rF   rG   r9   r9   r:   __hash__=   s   

zivmpf.__hash__c                 C      | S r=   r9   rK   r9   r9   r:   realD      z
ivmpf.realc                 C   s   | j jS r=   )r>   zerorK   r9   r9   r:   imagG      z
ivmpf.imagc                 C   rU   r=   r9   rK   r9   r9   r:   	conjugateJ   s    zivmpf.conjugatec                 C   s   | j \}}| j||fS r=   rC   r>   make_mpfrS   r9   r9   r:   rF   L      
zivmpf.ac                 C   s   | j \}}| j||fS r=   r\   rS   r9   r9   r:   rG   Q   r^   zivmpf.bc                 C   "   | j }t| j|j}|||fS r=   )r>   r   rC   r8   r]   rE   r>   vr9   r9   r:   midV      z	ivmpf.midc                 C   r_   r=   )r>   r   rC   r8   r]   r`   r9   r9   r:   delta\   rc   zivmpf.deltac                 C   s
   | j tfS r=   )rC   mpi_zerorK   r9   r9   r:   _mpci_b   s   
zivmpf._mpci_c                  G      t d)Nz-no ordering relation is defined for intervals	TypeError)argsr9   r9   r:   _comparef   rZ   zivmpf._comparec                 C   s$   | j |}| j|jko|j| jkS r=   )r>   mpfrF   rG   )rE   tr9   r9   r:   __contains__n      zivmpf.__contains__c                 C   s   t | j| jjS r=   )r   rC   r>   r8   rK   r9   r9   r:   __str__r      zivmpf.__str__c                 C   sJ   | j jrt| S | j\}}t| j j}t||}t||}d||f S )Nzmpi(%r, %r))r>   prettystrrC   r	   r8   r   Zto_str)rE   rF   rG   nr9   r9   r:   __repr__u   s   
zivmpf.__repr__c                 C   s8   t |dsz| j|}W n   t Y S || j|jS )NrC   )r3   r>   r?   NotImplementedrC   )srm   Zcmpfunr9   r9   r:   rk   ~   s   
c                 C      |  |tjS r=   )rk   r   Zmpi_eqrw   rm   r9   r9   r:   __eq__       zivmpf.__eq__c                 C   rx   r=   )rk   r   Zmpi_nery   r9   r9   r:   __ne__   r{   zivmpf.__ne__c                 C   rx   r=   )rk   r   Zmpi_ltry   r9   r9   r:   __lt__   r{   zivmpf.__lt__c                 C   rx   r=   )rk   r   Zmpi_lery   r9   r9   r:   __le__   r{   zivmpf.__le__c                 C   rx   r=   )rk   r   Zmpi_gtry   r9   r9   r:   __gt__   r{   zivmpf.__gt__c                 C   rx   r=   )rk   r   Zmpi_gery   r9   r9   r:   __ge__   r{   zivmpf.__ge__c                 C      | j t| j| j jS r=   )r>   r]   r   rC   r8   rK   r9   r9   r:   __abs__      zivmpf.__abs__c                 C   r   r=   )r>   r]   r   rC   r8   rK   r9   r9   r:   __pos__   r   zivmpf.__pos__c                 C   r   r=   )r>   r]   r   rC   r8   rK   r9   r9   r:   __neg__   r   zivmpf.__neg__Nc                 C      | j | |||S r=   r>   Zalmosteqrw   rm   Zrel_epsZabs_epsr9   r9   r:   ae      zivmpf.ae)r   NN)!__name__
__module____qualname____doc__rA   rH   rL   rO   rQ   rT   propertyrV   rY   r[   rF   rG   rb   rd   rf   rk   r   r~   r   rn   rp   ru   rz   r|   r}   r   r   r   r   r9   r9   r9   r:   r<   &   sT    







	r<   c                   @   s   e Zd Zd.ddZdd Zdd Zdd	 Zed
d Zedd Z	edd Z
edd Zedd Zedd Zdd Zdd Zdd Zd/ddZdd  Zd!d" Zd#d$ Ze Z ZZd%d& Zd'd( Zd)d* Zd0d,d-Zd+S )1ivmpcr   c                 C   s2   | j |}| j |}t| }|j|jf|_|S r=   )r>   r?   newrC   rf   )r@   reimyr9   r9   r:   rA      s
   zivmpc.__new__c                 C   s8   | j \\}}\}}||kr||krt||fS t| j S r=   )rf   r/   rR   rE   rF   rG   cdr9   r9   r:   rT      s   
zivmpc.__hash__c                 C   s(   | j jrt| S dt| jt| jf S )Nziv.mpc(%s, %s))r>   rr   rs   reprrV   rY   rw   r9   r9   r:   ru      s   zivmpc.__repr__c                 C   s   dt | jt | jf S )N(%s + %s*j))rs   rV   rY   r   r9   r9   r:   rp      r   zivmpc.__str__c                 C   s"   | j \\}}\}}| j||fS r=   rf   r>   r]   r   r9   r9   r:   rF         zivmpc.ac                 C   s"   | j \\}}\}}| j||fS r=   r   r   r9   r9   r:   rG      r   zivmpc.bc                 C   s"   | j \\}}\}}| j||fS r=   r   r   r9   r9   r:   r      r   zivmpc.cc                 C   s"   | j \\}}\}}| j||fS r=   r   r   r9   r9   r:   r      r   zivmpc.dc                 C      | j | jd S Nr   r>   r]   rf   r   r9   r9   r:   rV         z
ivmpc.realc                 C   r   Nr   r   r   r9   r9   r:   rY      r   z
ivmpc.imagc                 C   s   | j \}}| j|t|fS r=   )rf   r>   make_mpcr   )rw   rF   rG   r9   r9   r:   r[      s   
zivmpc.conjugatec                 C   s  | j |}| j|j  ko| jkn  pA| j|j  ko!| jkn  pA|j| j  ko0|jkn  pA|j| j  ko?|jkn  }| j|j  koN| jkn  p}| j|j  ko]| jkn  p}|j| j  kol|jkn  p}|j| j  ko{|jkn  }|o|S r=   )r>   r?   rF   rG   r   r   )rw   rm   Zreal_overlapZimag_overlapr9   r9   r:   overlap   s   xxzivmpc.overlapc                 C   s$   | j |}|j| jv o|j| jv S r=   )r>   r?   rV   rY   ry   r9   r9   r:   rn      ro   zivmpc.__contains__Fc                 C   sl   t || jjsz| j|}W n   t Y S t|dr"|jtf}nt|dr*|j}|r1| j|kS | j|kS NrC   rf   )	r4   r>   _typesr?   rv   r3   rC   re   rf   )rw   rm   netvalr9   r9   r:   rk      s   



zivmpc._comparec                 C   s
   |  |S r=   rk   ry   r9   r9   r:   rz         
 zivmpc.__eq__c                 C   s   |  |dS )NTr   ry   r9   r9   r:   r|      s    zivmpc.__ne__c                 C   rg   )Nz#complex intervals cannot be orderedrh   ry   r9   r9   r:   r}      s    zivmpc.__lt__c                 C   r   r=   )r>   r   r$   rf   r8   r   r9   r9   r:   r          zivmpc.__neg__c                 C   r   r=   )r>   r   r#   rf   r8   r   r9   r9   r:   r      r   zivmpc.__pos__c                 C   r   r=   )r>   r]   r*   rf   r8   r   r9   r9   r:   r      r   zivmpc.__abs__Nc                 C   r   r=   r   r   r9   r9   r:   r      r   zivmpc.ae)r   r   )Fr   )r   r   r   rA   rT   ru   rp   r   rF   rG   r   r   rV   rY   r[   r   rn   rk   rz   r|   r}   r~   r   r   r   r   r   r   r9   r9   r9   r:   r      s:    







r   c                    sZ    fddfddfdd}fdd}fd	d
}fdd}||||fS )Nc                    s   |   ||| jS r=   )r   r8   r>   Zsvalr   )	f_complexr9   r:   	g_complex   s   z_binary_op.<locals>.g_complexc                    sF   z|   ||| jW S  ty"   |tf}|tf}| || Y S w r=   )r]   r8   r-   re   r   )f_realr   r9   r:   g_real   s   z_binary_op.<locals>.g_realc                    sf   t |trtS | j}t ||js||}t|dr"|| j|jS t|dr1 || jtf|j	S tS r   )
r4   r0   rv   r>   r   r?   r3   rC   re   rf   rw   rm   r>   r   r   r9   r:   lop_real   s   z_binary_op.<locals>.lop_realc                    sX   | j }t||js||}t|dr||j| jS t|dr* ||j| jtfS tS r   )	r>   r4   r   r?   r3   rC   rf   re   rv   r   r   r9   r:   rop_real  s
   z_binary_op.<locals>.rop_realc              	      s^   t |trtS | j}t || jjs'z| j|}W n ttfy&   t Y S w  || j|jS r=   )	r4   r0   rv   r>   r   r?   rD   ri   rf   r   r   r9   r:   lop_complex  s   z_binary_op.<locals>.lop_complexc                    s0   | j }t|| j js| j |} ||j| jS r=   )r>   r4   r   r?   rf   r   r   r9   r:   rop_complex  s   z_binary_op.<locals>.rop_complexr9   )r   r   r   r   r   r   r9   )r   r   r   r   r:   
_binary_op   s   	r   c                   @   s$   e Zd Zdd Zdd ZeeZdS )ivmpf_constantc                 C   s   t | }||_|S r=   )r   _f)r@   frE   r9   r9   r:   rA   %  s   zivmpf_constant.__new__c                 C   s,   | j jd }| |t}| |t}||fS r   )r>   _precr   r
   r   )rE   r8   rF   rG   r9   r9   r:   	_get_mpi_)  s   zivmpf_constant._get_mpi_N)r   r   r   rA   r   r   rC   r9   r9   r9   r:   r   $  s    r   c                   @   s   e Zd Zdd Zd7ddZdd Zd7dd	Zed
d Zdd Z	dd Z
edd e	Zedd e
Zdd Zdd Zdd Zdd Zd8ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd9d5d6ZdS ):MPIntervalContextc                 C   s   t dtfi | _t dtfi | _| j| jf| _t dtfi | _dg| _| 	d | jt
| jg | j_ | j_| j_|  | j_ | j_| j_d| _t|  |   d S )Nr<   r   r   5   F)typer<   rl   r   mpcr   r   	_constantr   	_set_precr   Z_ctxdatar>   rr   r1   __init___init_builtinsr>   r9   r9   r:   r   2  s   
"
zMPIntervalContext.__init__Nc                 C   s    |d u r	|  |S |  ||fS r=   )rl   )r>   rF   rG   r9   r9   r:   _mpi?  s   
zMPIntervalContext._mpic                 C   s  |  d| _|  d| _|  d| _| j | _|  d| _| dd| _| t	j
t	j| _| t	j| _| t	jt	j| _| t	jt	j| _| t	jt	j| _| t	j| _| t	jt	j| _| t	jt	j| _| t	j t	j!| _"| t	j#t	j$| _%| j%| _&| 'dd | _(| 't	j)| _*| 't	j+| _,| 't	j-| _.| 't	j/| _0| 't	j1| _2| 't	j3| _4| 't	j5| _6| 't	j7| _8| 't	j9| _:| 't	j;| _<d S )Nr   r   infnanc                 S   s   dt d|  dfS Nr   r   )r   )r8   Zrndr9   r9   r:   <lambda>W  s    z2MPIntervalContext._init_builtins.<locals>.<lambda>)=rl   onerX   r   ninfr   r   j_wrap_mpi_functionr   Zmpi_expr+   expZmpi_sqrtZsqrtZmpi_logr,   ZlnZmpi_cosZmpci_cosZcosZmpi_sinZmpci_sinZsinZmpi_tanZtanZ	mpi_gammaZ
mpci_gammaZgammaZmpi_loggammaZmpci_loggammaZloggammaZ
mpi_rgammaZmpci_rgammaZrgammaZmpi_factorialZmpci_factorialZ	factorialZfacr   ZepsZmpf_piZpiZmpf_eeZmpf_ln2Zln2Zmpf_ln10Zln10Zmpf_phiZphiZ	mpf_eulerZeulerZmpf_catalancatalanZmpf_glaisherZglaisherZmpf_khinchinZkhinchinZmpf_twinprimeZ	twinprimer   r9   r9   r:   r   D  s8   
z MPIntervalContext._init_builtinsc                    s    fdd}|S )Nc                    sh   |r| d jd }n jd } | } t| dr$ | j|S t| dr2 | j|S t)Nr8   r   rC   rf   )	getr   r?   r3   r]   rC   r   rf   rD   )r7   kwargsr8   r>   r   r   r9   r:   gd  s   



z/MPIntervalContext._wrap_mpi_function.<locals>.gr9   )r>   r   r   r   r9   r   r:   r   c  s   z$MPIntervalContext._wrap_mpi_functionc                    s&   |r	 fdd}n }t | || d S )Nc                    sZ   | j   fdd|D }| j}z|  jd7  _| g|R i |}W || _|
 S || _w )Nc                    s   g | ]} |qS r9   r9   ).0rF   r?   r9   r:   
<listcomp>v  s    zFMPIntervalContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>
   )r?   r8   )r>   rj   r   r8   Zretvalr   r   r:   	f_wrappedt  s   z2MPIntervalContext._wrap_specfun.<locals>.f_wrapped)setattr)r@   namer   wrapr   r9   r   r:   _wrap_specfunq  s   zMPIntervalContext._wrap_specfunc                 C   s"   t dt|| jd< t|| _d S )Nr   r   )maxrJ   r   r   _dpsr>   rt   r9   r9   r:   r     s   zMPIntervalContext._set_precc                 C   s"   t || jd< tdt|| _d S r   )r   r   r   rJ   r   r   r9   r9   r:   _set_dps  s   zMPIntervalContext._set_dpsc                 C   s
   | j d S r   )r   r   r9   r9   r:   r     r   zMPIntervalContext.<lambda>c                 C   s   | j S r=   )r   r   r9   r9   r:   r     s    c                 C      t | j}||_|S r=   )r   rl   rC   r>   ra   rF   r9   r9   r:   r]        
zMPIntervalContext.make_mpfc                 C   r   r=   )r   r   rf   r   r9   r9   r:   r     r   zMPIntervalContext.make_mpcc                 C   s:   |\}}t ||| jt}t ||| jt}| ||fS r=   )r   r   r8   r
   r   r]   )r>   ZpqpqrF   rG   r9   r9   r:   _mpq  s   zMPIntervalContext._mpqc              	   C   sB  t || j| jfr|S t || jr|
 S t |tst|dr0| |j}| |j}| ||S t |t	r@t
|| j}| |S t|drK|j\}}n:z|\}}W n ttfy`   | }}Y nw t|drl|jd }nt|| jt}t|dr~|jd }nt|| jt}|tks|tkrt}t}t||sJ d| ||fS )NZ_mpc_rC   r   r   z"endpoints must be properly ordered)r4   rl   r   r   rP   r3   r?   rV   rY   r   r"   r8   r]   rC   ri   rD   r;   r
   r   r   r   r   r   )r>   r7   r   r   ra   rF   rG   r9   r9   r:   r?     s:   




zMPIntervalContext.convert   c                 K   sx   |  |}t|drtj|j|fi |S t|dr:tj|jd |fi |}tj|jd |fi |}d||f S d S )NrC   rf   r   r   r   )r?   r3   r   Z
mpi_to_strrC   rf   )r>   r7   rt   r   r   r   r9   r9   r:   nstr  s   


zMPIntervalContext.nstrc                 C   s~   |  |}t|| jrt| |j| |jd S t|j	\}}|\}}}}|r.|| S |t
kr5| jS |tkr<| jS | jS r   )r?   r4   r   r   magrV   rY   r   r   rC   r   r   r   r   r   r>   r7   rF   rG   signZmanr   Zbcr9   r9   r:   r     s   
zMPIntervalContext.magc                 C   s   dS )NFr9   r>   r7   r9   r9   r:   isnan  rW   zMPIntervalContext.isnanc                 C   s
   || j kS r=   )r   r   r9   r9   r:   isinf  s   
zMPIntervalContext.isinfc                 C   s@   |  |}|j\}}||kr|\}}}}|r|dkS |tkS d S r   )r?   rC   r   r   r9   r9   r:   isint  s   

zMPIntervalContext.isintc                 C   s6   |  |j\}}t||}t||}| ||fS r=   )r?   rC   r   Z	mpf_shiftr]   )r>   r7   rt   rF   rG   r9   r9   r:   ldexp  s   zMPIntervalContext.ldexpc                 C      t | |jS r=   )absr?   rF   r   r9   r9   r:   absmin  rq   zMPIntervalContext.absminc                 C   r   r=   )r   r?   rG   r   r9   r9   r:   absmax  rq   zMPIntervalContext.absmaxc                 C   s.   |  |j}|  |j}| t||| jS r=   )r?   rC   r]   r   Z	mpi_atan2r8   )r>   r   r7   r9   r9   r:   atan2  s   zMPIntervalContext.atan2c                 C   st   t |tjr
|dfS t |tr|\}}| || | dfS | |}t || jr.|dfS t || jr8|dfS t)NZRC)r4   r   r   tuplerl   r?   r   rD   )r>   r7   r   r   r9   r9   r:   _convert_param  s   

z MPIntervalContext._convert_paramc                 C   s   t || jp
t |tS r=   )r4   rl   r   r>   zr9   r9   r:   _is_real_type  s   zMPIntervalContext._is_real_typec                 C   s   t || jS r=   )r4   r   r   r9   r9   r:   _is_complex_type  rB   z"MPIntervalContext._is_complex_typep  c                 K   s   t |}t|}t||| }	| j }
}d}	 |D ]
}||| | 9 }q|	D ]
}||| |  }q&|d7 }|| }||9 }|
|7 }
|dkrG|
S ||krN| jqr   )listranger   ZNoConvergence)r>   r   r   typesZcoeffsr   Zmaxtermsr   ZnumZdenrw   rm   kir9   r9   r:   hypsum	  s   
 zMPIntervalContext.hypsumr=   )r   )r  )r   r   r   r   r   r   r   classmethodr   r   r   r   r8   Zdpsr]   r   r   r?   r   r   r   r   r   r   r   r   r   r   r  r  r	  r9   r9   r9   r:   r   0  s8    



!	
r   )Ooperator r   Zlibmp.backendr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   Zmatrices.matricesr0   re   Zctx_baser1   objectrA   r   r;   r<   r   r   __add____radd____sub____rsub____mul____rmul__Z__div__Z__rdiv____pow____rpow____truediv____rtruediv__r   r   ZnumbersZComplexregisterZRealImportErrorr9   r9   r9   r:   <module>   s:    q]' s