a
    <b;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 n eNy(   Y n0 dS )    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   sZ   t | dr| jS t| tr&t| ||S t| tr<t| ||S t| trRt| ||S t	d S )N_mpf_)
hasattrr2   
isinstancer   r   floatr   r   r   NotImplementedError)xprecrounding r:   ]/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-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   r:   r:   r;   __new__+   s    zivmpf.__new__c                 C   s&   | j \}}||kr|||S td S r>   )_mpi_
ValueError)selfrA   Z	f_convertabr:   r:   r;   cast.   s    
z
ivmpf.castc                 C   s   |  ttjS r>   )rH   intr   Zto_intrE   r:   r:   r;   __int__4   s    zivmpf.__int__c                 C   s   |  ttjS r>   )rH   r5   r   to_floatrJ   r:   r:   r;   	__float__7   s    zivmpf.__float__c                 C   s   |  ttjS r>   )rH   complexr   rL   rJ   r:   r:   r;   __complex__:   s    zivmpf.__complex__c                 C   s(   | j \}}||krt|S t| j S d S r>   )rC   r.   hashrE   rF   rG   r:   r:   r;   __hash__=   s    
zivmpf.__hash__c                 C   s   | S r>   r:   rJ   r:   r:   r;   realD   s    z
ivmpf.realc                 C   s   | j jS r>   )r?   zerorJ   r:   r:   r;   imagG   s    z
ivmpf.imagc                 C   s   | S r>   r:   rJ   r:   r:   r;   	conjugateJ       zivmpf.conjugatec                 C   s   | j \}}| j||fS r>   rC   r?   make_mpfrQ   r:   r:   r;   rF   L   s    
zivmpf.ac                 C   s   | j \}}| j||fS r>   rX   rQ   r:   r:   r;   rG   Q   s    
zivmpf.bc                 C   s"   | j }t| j|j}|||fS r>   )r?   r   rC   r8   rY   rE   r?   vr:   r:   r;   midV   s    z	ivmpf.midc                 C   s"   | j }t| j|j}|||fS r>   )r?   r   rC   r8   rY   rZ   r:   r:   r;   delta\   s    zivmpf.deltac                 C   s
   | j tfS r>   )rC   mpi_zerorJ   r:   r:   r;   _mpci_b   s    zivmpf._mpci_c                  G   s   t dd S )Nz-no ordering relation is defined for intervals	TypeError)argsr:   r:   r;   _comparef   s    zivmpf._comparec                 C   s$   | j |}| j|jko"|j| jkS r>   )r?   mpfrF   rG   )rE   tr:   r:   r;   __contains__n   s    zivmpf.__contains__c                 C   s   t | j| jjS r>   )r   rC   r?   r8   rJ   r:   r:   r;   __str__r   s    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   nr:   r:   r;   __repr__u   s    
zivmpf.__repr__c                 C   s:   t |ds,z| j|}W n   t Y S 0 || j|jS )NrC   )r3   r?   r@   NotImplementedrC   )sre   Zcmpfunr:   r:   r;   rc   ~   s    

c                 C   s   |  |tjS r>   )rc   r   Zmpi_eqrm   re   r:   r:   r;   __eq__   rW   zivmpf.__eq__c                 C   s   |  |tjS r>   )rc   r   Zmpi_nern   r:   r:   r;   __ne__   rW   zivmpf.__ne__c                 C   s   |  |tjS r>   )rc   r   Zmpi_ltrn   r:   r:   r;   __lt__   rW   zivmpf.__lt__c                 C   s   |  |tjS r>   )rc   r   Zmpi_lern   r:   r:   r;   __le__   rW   zivmpf.__le__c                 C   s   |  |tjS r>   )rc   r   Zmpi_gtrn   r:   r:   r;   __gt__   rW   zivmpf.__gt__c                 C   s   |  |tjS r>   )rc   r   Zmpi_gern   r:   r:   r;   __ge__   rW   zivmpf.__ge__c                 C   s   | j t| j| j jS r>   )r?   rY   r   rC   r8   rJ   r:   r:   r;   __abs__   s    zivmpf.__abs__c                 C   s   | j t| j| j jS r>   )r?   rY   r   rC   r8   rJ   r:   r:   r;   __pos__   s    zivmpf.__pos__c                 C   s   | j t| j| j jS r>   )r?   rY   r   rC   r8   rJ   r:   r:   r;   __neg__   s    zivmpf.__neg__Nc                 C   s   | j | |||S r>   r?   Zalmosteqrm   re   Zrel_epsZabs_epsr:   r:   r;   ae   s    zivmpf.ae)r   )NN)!__name__
__module____qualname____doc__rB   rH   rK   rM   rO   rR   propertyrS   rU   rV   rF   rG   r\   r]   r_   rc   rs   rr   rt   rf   rg   rk   ro   rp   rq   ru   rv   rw   rz   r:   r:   r:   r;   r=   &   sR   







	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   r_   )rA   reimyr:   r:   r;   rB      s
    zivmpc.__new__c                 C   s<   | j \\}}\}}||kr.||kr.t||fS t| j S d S r>   )r_   r/   rP   rE   rF   rG   cdr:   r:   r;   rR      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?   rh   ri   reprrS   rU   rm   r:   r:   r;   rk      s    zivmpc.__repr__c                 C   s   dt | jt | jf S )N(%s + %s*j))ri   rS   rU   r   r:   r:   r;   rg      s    zivmpc.__str__c                 C   s"   | j \\}}\}}| j||fS r>   r_   r?   rY   r   r:   r:   r;   rF      s    zivmpc.ac                 C   s"   | j \\}}\}}| j||fS r>   r   r   r:   r:   r;   rG      s    zivmpc.bc                 C   s"   | j \\}}\}}| j||fS r>   r   r   r:   r:   r;   r      s    zivmpc.cc                 C   s"   | j \\}}\}}| j||fS r>   r   r   r:   r:   r;   r      s    zivmpc.dc                 C   s   | j | jd S Nr   r?   rY   r_   r   r:   r:   r;   rS      s    z
ivmpc.realc                 C   s   | j | jd S Nr   r   r   r:   r:   r;   rU      s    z
ivmpc.imagc                 C   s   | j \}}| j|t|fS r>   )r_   r?   make_mpcr   )rm   rF   rG   r:   r:   r;   rV      s    
zivmpc.conjugatec                 C   s  | j |}| j|j  ko$| jkn  p| j|j  koB| jkn  p|j| j  ko`|jkn  p|j| j  ko~|jkn  }| j|j  ko| jkn  p| j|j  ko| jkn  p|j| j  ko|jkn  p|j| j  ko|jkn  }|o|S r>   )r?   r@   rF   rG   r   r   )rm   re   Zreal_overlapZimag_overlapr:   r:   r;   overlap   s    xxzivmpc.overlapc                 C   s$   | j |}|j| jv o"|j| jv S r>   )r?   r@   rS   rU   rn   r:   r:   r;   rf      s    zivmpc.__contains__Fc                 C   sn   t || jjs0z| j|}W n   t Y S 0 t|drF|jtf}nt|drV|j}|rd| j|kS | j|kS NrC   r_   )	r4   r?   _typesr@   rl   r3   rC   r^   r_   )rm   re   netvalr:   r:   r;   rc      s    



zivmpc._comparec                 C   s
   |  |S r>   rc   rn   r:   r:   r;   ro      rW   zivmpc.__eq__c                 C   s   |  |dS )NTr   rn   r:   r:   r;   rp      rW   zivmpc.__ne__c                 C   s   t dd S )Nz#complex intervals cannot be orderedr`   rn   r:   r:   r;   rq      rW   zivmpc.__lt__c                 C   s   | j t| j| j jS r>   )r?   r   r$   r_   r8   r   r:   r:   r;   rw      rW   zivmpc.__neg__c                 C   s   | j t| j| j jS r>   )r?   r   r#   r_   r8   r   r:   r:   r;   rv      rW   zivmpc.__pos__c                 C   s   | j t| j| j jS r>   )r?   rY   r*   r_   r8   r   r:   r:   r;   ru      rW   zivmpc.__abs__Nc                 C   s   | j | |||S r>   rx   ry   r:   r:   r;   rz      s    zivmpc.ae)r   r   )F)NN)r{   r|   r}   rB   rR   rk   rg   r   rF   rG   r   r   rS   rU   rV   r   rf   rc   ro   rp   rq   rr   rs   rt   rw   rv   ru   rz   r:   r:   r:   r;   r      s8   







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_complexr:   r;   	g_complex   s    z_binary_op.<locals>.g_complexc                    sJ   z|   ||| jW S  tyD   |tf}|tf}| || Y S 0 d S r>   )rY   r8   r-   r^   r   )f_realr   r:   r;   g_real   s    z_binary_op.<locals>.g_realc                    sf   t |trtS | j}t ||js*||}t|drD|| j|jS t|drb || jtf|j	S tS r   )
r4   r0   rl   r?   r   r@   r3   rC   r^   r_   rm   re   r?   r   r   r:   r;   lop_real   s    z_binary_op.<locals>.lop_realc                    sX   | j }t||js||}t|dr6||j| jS t|drT ||j| jtfS tS r   )	r?   r4   r   r@   r3   rC   r_   r^   rl   r   r   r:   r;   rop_real  s
    z_binary_op.<locals>.rop_realc              	      s^   t |trtS | j}t || jjsNz| j|}W n ttfyL   t Y S 0  || j|jS r>   )	r4   r0   rl   r?   r   r@   rD   ra   r_   r   r   r:   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@   r_   r   r   r:   r;   rop_complex  s    z_binary_op.<locals>.rop_complexr:   )r   r   r   r   r   r   r:   )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)rA   frE   r:   r:   r;   rB   %  s    zivmpf_constant.__new__c                 C   s,   | j jd }| |t}| |t}||fS r   )r?   _precr   r
   r   )rE   r8   rF   rG   r:   r:   r;   	_get_mpi_)  s    zivmpf_constant._get_mpi_N)r{   r|   r}   rB   r   r   rC   r:   r:   r:   r;   r   $  s   r   c                   @   s   e Zd Zdd Zd7ddZdd Zd8d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d9d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d:d5d6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=   rd   r   mpcr   r   	_constantr   	_set_precr   Z_ctxdatar?   rh   r1   __init___init_builtinsr?   r:   r:   r;   r   2  s    
"
zMPIntervalContext.__init__Nc                 C   s    |d u r|  |S |  ||fS r>   )rd   )r?   rF   rG   r:   r:   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rndr:   r:   r;   <lambda>W  rW   z2MPIntervalContext._init_builtins.<locals>.<lambda>)=rd   onerT   r   ninfr   r   j_wrap_mpi_functionr   Zmpi_expr+   expZmpi_sqrtsqrtZmpi_logr,   lnZmpi_cosZmpci_coscosZmpi_sinZmpci_sinsinZmpi_tantanZ	mpi_gammaZ
mpci_gammagammaZmpi_loggammaZmpci_loggammaZloggammaZ
mpi_rgammaZmpci_rgammaZrgammaZmpi_factorialZmpci_factorial	factorialZfacr   ZepsZmpf_pipiZmpf_eeZmpf_ln2Zln2Zmpf_ln10Zln10Zmpf_phiphiZ	mpf_eulerZeulerZmpf_catalancatalanZmpf_glaisherZglaisherZmpf_khinchinZkhinchinZmpf_twinprimeZ	twinprimer   r:   r:   r;   r   D  s8    
z MPIntervalContext._init_builtinsc                    s    fdd}|S )Nc                    sl   |r| d jd }n
 jd } | } t| drH | j|S t| drd | j|S td S )Nr8   r   rC   r_   )	getr   r@   r3   rY   rC   r   r_   rD   )r7   kwargsr8   r?   r   r   r:   r;   gd  s    



z/MPIntervalContext._wrap_mpi_function.<locals>.gr:   )r?   r   r   r   r:   r   r;   r   c  s    z$MPIntervalContext._wrap_mpi_functionc                    s&   |r fdd}n }t | || d S )Nc                    s\   | j   fdd|D }| j}z.|  jd7  _| g|R i |}W || _n|| _0 |
 S )Nc                    s   g | ]} |qS r:   r:   ).0rF   r@   r:   r;   
<listcomp>v  rW   zFMPIntervalContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>
   )r@   r8   )r?   rb   r   r8   retvalr   r   r;   	f_wrappedt  s    z2MPIntervalContext._wrap_specfun.<locals>.f_wrapped)setattr)rA   namer   wrapr   r:   r   r;   _wrap_specfunq  s    zMPIntervalContext._wrap_specfunc                 C   s"   t dt|| jd< t|| _d S )Nr   r   )maxrI   r   r   _dpsr?   rj   r:   r:   r;   r     s    zMPIntervalContext._set_precc                 C   s"   t || jd< tdt|| _d S r   )r   r   r   rI   r   r   r:   r:   r;   _set_dps  s    zMPIntervalContext._set_dpsc                 C   s
   | j d S r   )r   r   r:   r:   r;   r     rW   zMPIntervalContext.<lambda>c                 C   s   | j S r>   )r   r   r:   r:   r;   r     rW   c                 C   s   t | j}||_|S r>   )r   rd   rC   r?   r[   rF   r:   r:   r;   rY     s    
zMPIntervalContext.make_mpfc                 C   s   t | j}||_|S r>   )r   r   r_   r   r:   r:   r;   r     s    
zMPIntervalContext.make_mpcc                 C   s:   |\}}t ||| jt}t ||| jt}| ||fS r>   )r   r   r8   r
   r   rY   )r?   ZpqpqrF   rG   r:   r:   r;   _mpq  s    zMPIntervalContext._mpqc              	   C   sH  t || j| jfr|S t || jr(|
 S t |ts<t|dr`| |j}| |j}| ||S t |t	rt
|| j}| |S t|dr|j\}}ntz|\}}W n ttfy   | }}Y n0 t|dr|jd }nt|| jt}t|dr|jd }nt|| jt}|tks|tkr&t}t}t||s:J d| ||fS )NZ_mpc_rC   r   r   z"endpoints must be properly ordered)r4   rd   r   r   rN   r3   r@   rS   rU   r   r"   r8   rY   rC   ra   rD   r<   r
   r   r   r   r   r   )r?   r7   r   r   r[   rF   rG   r:   r:   r;   r@     s8    




zMPIntervalContext.convert   c                 K   sx   |  |}t|dr*tj|j|fi |S t|drttj|jd |fi |}tj|jd |fi |}d||f S d S )NrC   r_   r   r   r   )r@   r3   r   Z
mpi_to_strrC   r_   )r?   r7   rj   r   r   r   r:   r:   r;   nstr  s    


zMPIntervalContext.nstrc                 C   s~   |  |}t|| jr4t| |j| |jd S t|j	\}}|\}}}}|r\|| S |t
krj| jS |tkrx| jS | jS r   )r@   r4   r   r   magrS   rU   r   r   rC   r   r   r   r   r   r?   r7   rF   rG   signZmanr   Zbcr:   r:   r;   r     s    
zMPIntervalContext.magc                 C   s   dS )NFr:   r?   r7   r:   r:   r;   isnan  s    zMPIntervalContext.isnanc                 C   s
   || j kS r>   )r   r   r:   r:   r;   isinf  s    zMPIntervalContext.isinfc                 C   s@   |  |}|j\}}||kr<|\}}}}|r4|dkS |tkS d S r   )r@   rC   r   r   r:   r:   r;   isint  s    

zMPIntervalContext.isintc                 C   s6   |  |j\}}t||}t||}| ||fS r>   )r@   rC   r   Z	mpf_shiftrY   )r?   r7   rj   rF   rG   r:   r:   r;   ldexp  s    zMPIntervalContext.ldexpc                 C   s   t | |jS r>   )absr@   rF   r   r:   r:   r;   absmin  s    zMPIntervalContext.absminc                 C   s   t | |jS r>   )r   r@   rG   r   r:   r:   r;   absmax  s    zMPIntervalContext.absmaxc                 C   s.   |  |j}|  |j}| t||| jS r>   )r@   rC   rY   r   Z	mpi_atan2r8   )r?   r   r7   r:   r:   r;   atan2  s    zMPIntervalContext.atan2c                 C   sx   t |tjr|dfS t |tr>|\}}| || | dfS | |}t || jr\|dfS t || jrp|dfS td S )NZRC)r4   r   r   tuplerd   r@   r   rD   )r?   r7   r   r   r:   r:   r;   _convert_param  s    

z MPIntervalContext._convert_paramc                 C   s   t || jpt |tS r>   )r4   rd   r   r?   zr:   r:   r;   _is_real_type  s    zMPIntervalContext._is_real_typec                 C   s   t || jS r>   )r4   r   r   r:   r:   r;   _is_complex_type  s    z"MPIntervalContext._is_complex_typep  c                 K   s   t |}t|}t||| }	| j }
}d}|D ]}||| | 9 }q0|	D ]}||| |  }qJ|d7 }|| }||9 }|
|7 }
|dkr|
S ||kr,| jq,d S r   )listranger   ZNoConvergence)r?   r   r   typesZcoeffsr   Zmaxtermsr   numZdenrm   re   kir:   r:   r;   hypsum	  s    
 zMPIntervalContext.hypsum)N)N)r   )r   )r{   r|   r}   r   r   r   r   classmethodr   r   r   r   r8   ZdpsrY   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   r   0  s6   


!
	
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   r^   Zctx_baser1   objectrB   r   r<   r=   r   r   __add____radd____sub____rsub____mul____rmul__Z__div__Z__rdiv____pow____rpow____truediv____rtruediv__r   r   ZnumbersComplexregisterRealImportErrorr:   r:   r:   r;   <module>   s6   q]' s