o
    lhh                  	   @   s  d Z ddlZddlmZ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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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZR e efZSeefZTe!efZUe"efZVe#e$fZWe#e$e%fZXdd ZYd	d
 ZZdd Z[defddZ\dd Z]efddZ^dd Z_efddZ`efddZadefddZbdefddZcefddZddefd d!Zed"d# Zfefd$d%Zgefd&d'Zhefd(d)Ziefd*d+Zjefd,d-Zkefd.d/Zlefd0d1Zmefd2d3Znefd4d5Zoefd6d7Zpefd8d9Zqefd:d;Zrefd<d=Zsefd>d?Ztefd@dAZudBdC ZvefdDdEZwefdFdGZxefdHdIZyefdJdKZzdLdM Z{efdNdOZ|efdPdQZ}efdRdSZ~efdTdUZefdVdWZefdXdYZefdZd[Zefd\d]Zefd^d_Zefd`daZefdbdcZefdddeZefdfdgZefdhdiZefdjdkZedlZedmZdndo ZefdpdqZefdrdsZefdtduZefdvdwZefdxdyZefdzd{Zdd}d~ZdddZdddZdddZedkrWzddlm  m  mZ ej~Z~ejzZzW dS  eefyV   ed Y dS w dS )z-
Low-level functions for complex arithmetic.
    N   )MPZMPZ_ZEROMPZ_ONEMPZ_TWOBACKEND)1round_floorround_ceiling
round_downround_upround_nearest
round_fastbitcountbctable	normalize
normalize1reciprocal_rndrshiftlshiftgiant_stepsnegative_rndto_strto_fixedfrom_man_exp
from_floatto_floatfrom_intto_intfzerofoneftwofhalffinffninffnanfnonempf_absmpf_posmpf_negmpf_addmpf_submpf_mulmpf_divmpf_mul_int	mpf_shiftmpf_sqrt	mpf_hypotmpf_rdiv_int	mpf_floormpf_ceilmpf_nintmpf_fracmpf_signmpf_hashComplexResult)mpf_pimpf_expmpf_logmpf_cos_sinmpf_cosh_sinhmpf_tanmpf_pow_intmpf_log_hypotmpf_cos_sin_pimpf_phimpf_cosmpf_sin
mpf_cos_pi
mpf_sin_pimpf_atan	mpf_atan2mpf_coshmpf_sinhmpf_tanhmpf_asinmpf_acos	mpf_acoshmpf_nthrootmpf_fibonaccic                 C   $   | \}}|t v r
dS |t v rdS dS )z2Check if either real or imaginary part is infiniteTF)_infszreim rW   g/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/mpmath/libmp/libmpc.py
mpc_is_inf)      rY   c                 C   rQ   )z9Check if either real or imaginary part is infinite or nanTF)	_infs_nanrS   rW   rW   rX   mpc_is_infnan0   rZ   r\   c                 K   sZ   | \}}t ||}|d r|d t t||fi | d S |d t ||fi | d S )Nr   z - jz + )r   r(   )rT   dpskwargsrU   rV   rsrW   rW   rX   
mpc_to_str7   s
   
"ra   Fc                 C   s"   | \}}t t|||t|||S N)complexr   )rT   strictrndrU   rV   rW   rW   rX   mpc_to_complex?   s   rf   c                 C   sp   t jdkr!| \}}t|t jjt|  }|dt jj  }t|S z	tt| ddW S  t	y7   t|  Y S w )N)      rh   T)rd   )
sysversion_infor7   	hash_infoimagwidthinthashrf   OverflowError)rT   rU   rV   hrW   rW   rX   mpc_hashC   s   
rr   c                 C   s   | \}}|t |||fS rb   r(   rT   precre   rU   rV   rW   rW   rX   mpc_conjugateP   s   rv   c                 C   s   | t kS rb   )mpc_zero)rT   rW   rW   rX   mpc_is_nonzeroT   s   rx   c                 C   ,   | \}}|\}}t ||||t ||||fS rb   r)   rT   wru   re   abcdrW   rW   rX   mpc_addW      r   c                 C      | \}}t |||||fS rb   rz   )rT   xru   re   r}   r~   rW   rW   rX   mpc_add_mpf\      r   c                 C   ry   rb   r*   r{   rW   rW   rX   mpc_sub`   r   r   c                 C   r   rb   r   )rT   pru   re   r}   r~   rW   rW   rX   mpc_sub_mpfe   r   r   c                 C       | \}}t |||t |||fS rb   )r'   rT   ru   re   r}   r~   rW   rW   rX   mpc_posi      r   c                 C   r   rb   rs   r   rW   rW   rX   mpc_negm   r   r   c                 C   s   | \}}t ||t ||fS rb   )r.   )rT   nr}   r~   rW   rW   rX   	mpc_shiftq   s   r   c                 C   s   | \}}t ||||S )zEAbsolute value of a complex number, |a+bi|.
    Returns an mpf value.)r0   r   rW   rW   rX   mpc_absu   s   r   c                 C   s   | \}}t ||||S )z3Argument of a complex number. Returns an mpf value.)rH   r   rW   rW   rX   mpc_arg{   s   r   c                 C   r   rb   )r2   r   rW   rW   rX   	mpc_floor   r   r   c                 C   r   rb   )r3   r   rW   rW   rX   mpc_ceil   r   r   c                 C   r   rb   )r4   r   rW   rW   rX   mpc_nint   r   r   c                 C   r   rb   )r5   r   rW   rW   rX   mpc_frac   r   r   c                 C   s\   | \}}|\}}t ||}t ||}	t ||}
t ||}t||	||}t|
|||}||fS )z
    Complex multiplication.

    Returns the real and imaginary part of (a+bi)*(c+di), rounded to
    the specified precision. The rounding mode applies to the real and
    imaginary parts separately.
    )r+   r*   r)   )rT   r|   ru   re   r}   r~   r   r   r   qrsrU   rV   rW   rW   rX   mpc_mul   s   



r   c           
      C   sJ   | \}}t ||}t ||}t ||||}t||||}t|d}	||	fS Nr   )r+   r*   r.   )
rT   ru   re   r}   r~   r   r   r   rU   rV   rW   rW   rX   
mpc_square   s   


r   c                 C   ,   | \}}t ||||}t ||||}||fS rb   )r+   rT   r   ru   re   r}   r~   rU   rV   rW   rW   rX   mpc_mul_mpf      r   c                 C   s0   | \}}t t||||}t||||}||fS )zB
    Multiply the mpc value z by I*x where x is an mpf value.
    )r(   r+   )rT   r   ru   re   r}   r~   rU   rV   rW   rW   rX   mpc_mul_imag_mpf   s   r   c                 C   r   rb   )r-   )rT   r   ru   re   r}   r~   rU   rV   rW   rW   rX   mpc_mul_int   r   r   c                 C   s|   | \}}|\}}|d }t t||t|||}	t t||t|||}
tt||t|||}t|
|	||t||	||fS N
   )r)   r+   r*   r,   )rT   r|   ru   re   r}   r~   r   r   wpmagturW   rW   rX   mpc_div   s   r   c                 C   r   )zCalculate z/p where p is real)r,   r   rW   rW   rX   mpc_div_mpf   s   r   c                 C   sL   | \}}t t||t|||d }t||||}tt||||}||fS )zCalculate 1/z efficientlyr   r)   r+   r,   r(   )rT   ru   re   r}   r~   mrU   rV   rW   rW   rX   mpc_reciprocal   s
   r   c           	      C   sX   |\}}t t||t|||d }tt|| |||}ttt|| |||}||fS )z)Calculate p/z where p is real efficientlyr   r   )	r   rT   ru   re   r}   r~   r   rU   rV   rW   rW   rX   mpc_mpf_div   s
   r   c                 C   sp   d}d}|r4|d@ r||  ||  ||  ||  }}|d8 }| |  ||  d|  | } }|d }|s||fS )zgComplex integer power: computes (a+b*I)**n exactly for
    nonnegative n (a and b must be Python ints).r   r   rh   rW   )r}   r~   r   wrewimrW   rW   rX   complex_int_pow   s   "r   c                 C   s@   |d t krt| |d ||S ttt| |d ||d ||S )Nr   r   r   )r   mpc_pow_mpfmpc_expr   mpc_log)rT   r|   ru   re   rW   rW   rX   mpc_pow   s   "r   c           	      C   s|   |\}}}}|dkrt | d| ||>  ||S |dkr-t| |d }t |d| | ||S ttt| |d ||d ||S )Nr   r   )mpc_pow_intmpc_sqrtr   r   r   )	rT   r   ru   re   psignpmanpexppbcsqrtzrW   rW   rX   r      s   "r   c                 C   s  | \}}|t krt||||t fS |t krDt||||}|d; }|dkr(|t fS |dkr0t |fS |dkr:t|t fS |dkrDt t|fS |dkrJtS |dkrTt| ||S |dkr^t| ||S |dkrht| ||S |dk rytt| | |d ||S |\}}}	}
|\}}}}|r| }|r| }|	| }t|}||t	|
|  }|dk r|dkr||K }|}	n|| K }|	}t
|||\}}t|t||	 ||}t|t|| ||}||fS ttt| |d ||d ||S )	N   r   r   rh   rg   r   i'  r   )r   r?   r(   mpc_oner   r   r   r   absmaxr   r   rn   r   r   r   )rT   r   ru   re   r}   r~   vasignamanaexpabcbsignbmanbexpbbcdeabs_de
exact_sizerU   rV   rW   rW   rX   r      sJ   "


"r   c                 C   s(  | \}}|t kr*|t kr||fS |d r tt|||}t |fS t|||}|t fS |d }|d s]tt||f|||}t|d}	t|	||}t|d}
t|
|}t||||}||fS tt||f|||}t|d}	t|	||}t|d}
t|
|}t||||}|d rt|}t|}||fS )zComplex square root (principal branch).

    We have sqrt(a+bi) = sqrt((r+a)/2) + b/sqrt(2*(r+a))*i where
    r = abs(a+bi), when a+bi is not a negative real number.r      r   r   )r   r/   r(   r)   r   r.   r,   r*   )rT   ru   re   r}   r~   rV   rU   r   r   r   r   r|   rW   rW   rX   r   '  s8   





r   c                 C   s  d}t t| |||  }t t||||  }z|d|  d|  }|j}|j}	tt |}tt |	}	W n1 tyg   t||}t||}t|}
td|
|}t||f|t	f|\}}	t
|}t
|	}	Y nw d}|}|}t||| D ]}t||	|d \}}t||d | | | }t||d | | | }|| ||  || ? }t| || }t||| }|| ||  |? }| | ||  |? }||> | }||> | }||d t|||   | }||d t|	||   | }	|}qu||	fS )N2   y              ?g      ?r   r   )rn   r   realrl   r   rp   r   r1   r   r   r   r   r   r   )r}   r~   r   ru   starta1b1r   rU   rV   fnnthextraprevpextra1r   re2im2r4apbprecimcrebimbrW   rW   rX   mpc_nthroot_fixedK  sH   

r   c                 C   s  | \}}|d dkr|t krt||||}|t fS |dk rQ|dkr#tS |dkr/t||f||S |dkr<tt||f||S t||f| |d t| }tt|||S |dkrtd|d  }|\}	}
}}|\}}}}t||f|}|d	 |d  d
kr|d	 |d  |k rt	||}t	||}t
||||\}}d}t|| | ||}t|| | ||}||fS t|}|d d }td||}t||f|t f||\}}t|d |d |d |d ||}t|d |d |d |d ||}||fS )zu
    Complex n-th root.

    Use Newton method as in the real case when it is faster,
    otherwise use z**(1/n)
    r   rh   r   r      r   g333333?r   irg   )r   rO   r   r   r   mpc_nthrootr   rn   r   r   r   r   r   r1   r   r   )rT   r   ru   re   r}   r~   rU   inverseprec2r   r   r   r   r   r   r   r   pfafbfrV   r   r   r   rW   rW   rX   r   r  sB   (

""r   c                 C   s   t | d||S )z
    Complex cubic root.
    rg   )r   rT   ru   re   rW   rW   rX   mpc_cbrt  s   r   c           
      C   s|   | \}}|t krt|||S |t krt|||t fS t||d |}t||d |\}}t||||}t||||}	||	fS )av  
    Complex exponential function.

    We use the direct formula exp(a+bi) = exp(a) * (cos(b) + sin(b)*i)
    for the computation. This formula is very nice because it is
    pefectly stable; since we just do real multiplications, the only
    numerical errors that can creep in are single-ulp rounding errors.

    The formula is efficient since mpmath's real exp is quite fast and
    since we can compute cos and sin simultaneously.

    It is no problem if a and b are large; if the implementations of
    exp/cos/sin are accurate and efficient for all real numbers, then
    so is this function for all complex numbers.
    r   )r   r<   r:   r+   )
rT   ru   re   r}   r~   r   r   r   rU   rV   rW   rW   rX   r     s   r   c                 C   s*   t | d | d ||}t| ||}||fS )Nr   r   )r@   r   rt   rW   rW   rX   r     s   r   c                 C   s   | \}}|t krt|||t fS |t krt|||t fS |d }t||\}}t||\}}	t||||}
t||	||}|
t|fS )aS  Complex cosine. The formula used is cos(a+bi) = cos(a)*cosh(b) -
    sin(a)*sinh(b)*i.

    The same comments apply as for the complex exp: only real
    multiplications are pewrormed, so no cancellation errors are
    possible. The formula is also efficient since we can compute both
    pairs (cos, sin) and (cosh, sinh) in single stwps.   )r   rC   rI   r<   r=   r+   r(   rT   ru   re   r}   r~   r   r   r   chshrU   rV   rW   rW   rX   mpc_cos  s   r   c                 C   s   | \}}|t krt|||t fS |t krt t|||fS |d }t||\}}t||\}}	t||||}
t||	||}|
|fS )zComplex sine. We have sin(a+bi) = sin(a)*cosh(b) +
    cos(a)*sinh(b)*i. See the docstring for mpc_cos for additional
    comments.r   )r   rD   rJ   r<   r=   r+   r   rW   rW   rX   mpc_sin  s   r   c                 C   s   | \}}|\}}}}|\}	}
}}|t krt|||t fS |t kr(t t|||fS |d }t|d}t|d}t||\}}t||\}}t|||}t||||}t||||}||fS )zcComplex tangent. Computed as tan(a+bi) = sin(2a)/M + sinh(2b)/M*i
    where M = cos(2a) + cosh(2b).   r   )r   r>   rK   r.   r<   r=   r)   r,   )rT   ru   re   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   rV   rW   rW   rX   mpc_tan  s   

r   c                 C   s   | \}}|t krt|||t fS t|t|d |d }|t kr(t|||t fS |d }t||\}}t||\}}	t||||}
t||	||}|
t|fS Nr   r   )r   rE   r+   r9   rI   rA   r=   r(   r   rW   rW   rX   
mpc_cos_pi  s   r   c                 C   s   | \}}|t krt|||t fS t|t|d |d }|t kr(t t|||fS |d }t||\}}t||\}}	t||||}
t||	||}|
|fS r   )r   rF   r+   r9   rJ   rA   r=   r   rW   rW   rX   
mpc_sin_pi  s   r   c                 C   s   | \}}|t krt|||\}}|t ft |ffS |t kr,t|||\}}|t f|t ffS |d }	t||	\}}t||	\}}t||||}
t||||}t||||}t||||}|
t|f||ffS )Nr   )r   r=   r<   r+   r(   )rT   ru   re   r}   r~   r   r   r   r   r   crecimsresimrW   rW   rX   mpc_cos_sin  s   r   c                 C   s   | \}}|t krt|||\}}|t f|t ffS t|t|d |d }|t kr8t|||\}}|t ft |ffS |d }	t||	\}}t||	\}}t||||}
t||||}t||||}t||||}|
t|f||ffS r   )r   rA   r+   r9   r=   r(   )rT   ru   re   r}   r~   r   r   r   r   r   r   r   r   r   rW   rW   rX   mpc_cos_sin_pi%  s    r   c                 C   s   | \}}t |t|f||S )z:Complex hyperbolic cosine. Computed as cosh(z) = cos(z*i).)r   r(   r   rW   rW   rX   mpc_cosh7  s   r   c                 C   $   | \}}t ||f||\}}||fS )z;Complex hyperbolic sine. Computed as sinh(z) = -i*sin(z*i).)r   r   rW   rW   rX   mpc_sinh<     r  c                 C   r   )z>Complex hyperbolic tangent. Computed as tanh(z) = -i*tan(z*i).)r   r   rW   rW   rX   mpc_tanhB  r  r  c                 C   s   | \}}|d }t t||t|f}tt|||f}t||}t||}	t||	||\}}tt|dt|df}
|
d tkrIt| rI|
d t	f}
|
S )Nr   r   r   r   )
r)   r   r(   r*   r   r   r.   r$   rY   r   )rT   ru   re   r}   r~   r   r   yl1l2r   rW   rW   rX   mpc_atanI  s   

r  g:pΈ?g      ?c                 C   s"  | \}}|d }|t krlttt||}|d s,|dkr$t|||t fS t|||t fS |d rPt||}tt|||}	|dkrG|t|	fS tt	|d|	fS t|||}	|dkr^t |	fS t||}t	|dt|	fS d }
}|d rzt|}d}
|d rt|}d}tt||}t
t||}t|||}t|||}t	t
|||d}t|||}t|||}tt||d s|dkrt||}nt||}nt
|||}|d st|t
||||}	t
|||}t	t|t
|	|||d}|dkrttt|||||}nKtt|t||||}n>t|t
||||}	t|t||||}t	t
|	||d}t|t|||}|dkrEtt||||}n	tt||||}tt||d st|t
||||}t|d r~t
|||}t|||}t	t
|||d}nt|||}t	t
|||d}t|t
|t||}tt
tt
|t|||||}nttt|||t||}tt
||||}|
r|dkrtt|||}nt|}|s|dkrt|}|r|dkrt|}t|d |d |d |d ||}t|d |d |d |d ||}||fS )a&   complex acos for n = 0, asin for n = 1
    The algorithm is described in
    T.E. Hull, T.F. Fairgrieve and P.T.P. Tang
    'Implementing the Complex Arcsine and Arcosine Functions
    using Exception Handling',
    ACM Trans. on Math. Software Vol. 23 (1997), p299
    The complex acos and asin can be defined as
    acos(z) = acos(beta) - I*sign(a)* log(alpha + sqrt(alpha**2 -1))
    asin(z) = asin(beta) + I*sign(a)* log(alpha + sqrt(alpha**2 -1))
    where z = a + I*b
    alpha = (1/2)*(r + s); beta = (1/2)*(r - s) = a/alpha
    r = sqrt((a+1)**2 + y**2); s = sqrt((a-1)**2 + y**2)
    These expressions are rewritten in different ways in different
    regions, delimited by two crossovers alpha_crossover and beta_crossover,
    and by abs(a) <= 1, in order to improve the numerical accuracy.
    r   r   r   r   rh   rg   )r   r*   r   r&   rM   rL   r9   rN   r(   r.   r)   r0   r,   r+   beta_crossoverrG   r/   alpha_crossoverr;   r   )rT   ru   re   r   r}   r~   r   ampir   r   r   r   r   r   alphabetab2rU   Axr   c1c2Am1rV   rW   rW   rX   	acos_asin_  s   




"
""r  c                 C      t | ||dS Nr   r  r   rW   rW   rX   mpc_acos     r  c                 C   r  r   r  r   rW   rW   rX   mpc_asin  r  r  c                 C   s,   | \}}t |t|f||\}}t||fS rb   )r  r(   r   rW   rW   rX   	mpc_asinh  s   r  c                 C   s8   t | ||\}}|d s|tkrt||fS |t|fS r  )r  r   r(   r   rW   rW   rX   	mpc_acosh  s   r  c                 C   sj   |d }t | t|}tt| |}t||}t||}tt|||d}|d tkr3t| r3t|d f}|S )Nr   r   r   r   )r   r   r   r   r   r$   rY   r   )rT   ru   re   r   r}   r~   r   rW   rW   rX   	mpc_atanh  s   

r  c                 C   s   | \}}|t krt|||t fS tt|d |d  t|d |d  }|| d }t|}tt|dt|}t|t f| |}	t	| |}
t
|
|	|}
t|	|
|}	t|	|||}	|	S )Nrh   rg   r   r   )r   rP   r   r   rB   r)   r.   r%   r   r   r   r   r   )rT   ru   re   rU   rV   sizer   r}   r~   r   r   rW   rW   rX   mpc_fibonacci
  s   *
r  fc                 C      t rb   r8   r   ru   re   rW   rW   rX   mpf_expj     r#  c                 C   s   | \}}|t krt|||S |t krtt|||t fS tt||d }t||d \}}t||||}t||||}||fS r   )r   r<   r:   r(   r+   )rT   ru   re   rU   rV   eyr   r   rW   rW   rX   mpc_expj  s   r&  c                 C   r   rb   r!  r"  rW   rW   rX   
mpf_expjpi(  r$  r'  c                 C   s   | \}}|t krt|||S |\}}}}|d }	|r#|	td|| 7 }	ttt|	||	}|t kr9t|||t fS t||d }
t||d \}}t|
|||}t|
|||}||fS )Nr   r   )r   rA   r   r(   r+   r9   r:   )rT   ru   re   rU   rV   signmanexpbcr   r%  r   r   rW   rW   rX   
mpc_expjpi+  s   r,  sagez&Warning: Sage imports in libmpc failed)r  )__doc__ri   backendr   r   r   r   r   libmpfr   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   	libelefunr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r   rw   mpc_twompc_halfrR   r[   rY   r\   ra   rf   rr   rv   rx   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  r  r	  r  r  r  r  r  r  r  r#  r&  r'  r,  sage.libs.mpmath.ext_libmplibsmpmath	ext_libmp_lbmpImportErrorAttributeErrorprintrW   rW   rW   rX   <module>   s    h


		($') 		




