o
    lÇh¼  ã                   @   s  d Z ddlZddlZddlmZ dejv r ej d¡ dejd< dZ	ejd  
d	¡s/ejd Z	dd
lT ddlmZ ddd„Zd\ZZZZZdd„ ZdZdd„ ZedkrŒzddlmZ edƒjZedƒ W n eys   edƒ eZY nw eƒ Ze ¡ Z eee ƒ eƒ Z!ede!e ƒ dS dS )a  
Torture tests for asymptotics and high precision evaluation of
special functions.

(Other torture tests may also be placed here.)

Running this file (gmpy recommended!) takes several CPU minutes.
With Python 2.6+, multiprocessing is used automatically to run tests
in parallel if many cores are available. (A single test may take between
a second and several minutes; possibly more.)

The idea:

* We evaluate functions at positive, negative, imaginary, 45- and 135-degree
  complex values with magnitudes between 10^-20 to 10^20, at precisions between
  5 and 150 digits (we can go even higher for fast functions).

* Comparing the result from two different precision levels provides
  a strong consistency check (particularly for functions that use
  different algorithms at different precision levels).

* That the computation finishes at all (without failure), within reasonable
  time, provides a check that evaluation works at all: that the code runs,
  that it doesn't get stuck in an infinite loop, and that it doesn't use
  some extremely slowly algorithm where it could use a faster one.

TODO:

* Speed up those functions that take long to finish!
* Generalize to test more cases; more options.
* Implement a timeout mechanism.
* Some functions are notably absent, including the following:
  * inverse trigonometric functions (some become inaccurate for complex arguments)
  * ci, si (not implemented properly for large complex arguments)
  * zeta functions (need to modify test not to try too large imaginary values)
  * and others...

é    N)Údefault_timerz-nogmpyÚYÚMPMATH_NOGMPYÚ éÿÿÿÿz.py)Ú*)Úexec_é–   Fc                    s|  g d¢}‡ fdd„|D ƒ}dd„ }t ddƒ}|r|g d¢7 }|D ]”}|r*tj d	¡ d
t_tdƒ| d }| }	|t }
|dt  }|dt  }t t|ƒƒD ]e}|rZt	d||  ƒ || t_| |ƒ| |	ƒ| |
ƒ| |ƒ| |ƒf}|dkr±||d  }||d |d ||ƒ ||d |d ||	ƒ ||d |d ||
ƒ ||d |d ||ƒ ||d |d ||ƒ |}qNq |r¼t	ƒ  d S d S )N)
é   é   é   é2   éZ   r	   iô  iÜ  iˆ  i'  c                    s   g | ]}|ˆ kr|‘qS © r   )Ú.0Úp©Úmaxdpsr   úh/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/mpmath/tests/torture.pyÚ
<listcomp>9   s    ztest_asymp.<locals>.<listcomp>c                 S   s–   t | | ƒt |ƒ tdƒtƒd| d ƒk rd S tƒ  tdƒ td|ƒ td|ƒ td| ƒ td|ƒ td	t | | ƒƒ td
t | | ƒt |ƒ ƒ t‚)Né   é
   é   zError!zInput:zdps =z	Result 1:z	Result 2:zAbsolute error:zRelative error:)ÚabsÚworkprecÚpowerÚprintÚAssertionError)ÚxÚyr   Úinptr   r   r   Úcheck:   s   ,



ztest_asymp.<locals>.checkiìÿÿÿr   )iüÿÿiœÿÿÿiÎÿÿÿr   éd   iè  z. r   r   gWì/»'ò?r   r   zTesting dps = %sr   é   é   é   )
ÚrangeÚsysÚstdoutÚwriteÚmpÚdpsÚmpfÚjÚlenr   )Úfr   ÚverboseÚ
huge_ranger+   r!   Ú	exponentsÚnÚxposÚxnegÚximagÚ	xcomplex1Ú	xcomplex2ÚiÚnewr   Úprevr   r   r   Ú
test_asymp7   s@   

"ô
ÿr<   )g      ø?g      Àg      	@r%   r#   c                  C   sn   t dƒ\} }| d dksJ ‚|dksJ ‚dt_ttdd ƒƒdks#J ‚d	t_ttdd ƒƒd
ks2J ‚dt_d S )Ni(#  l    d(	 l   £cÈ| l
   fTè™-âµÛkfKßx] r   r   r"   zy-2.58183325604736e+987675256497386331227838638980680030172857347883537824464410652557820800494271520411283004120790908623r   zœ-2.5818332560473632073252488656039475548106223822913e+987675256497386331227838638980680030172857347883537824464410652557820800494271520411283004120790908623)Úbernfracr*   r+   ÚstrÚ	bernoulli)r   Úqr   r   r   Útest_bernoulli_huged   s   
rA   a  test_bernoulli_huge()
test_asymp(lambda z: +pi, maxdps=10000)
test_asymp(lambda z: +e, maxdps=10000)
test_asymp(lambda z: +ln2, maxdps=10000)
test_asymp(lambda z: +ln10, maxdps=10000)
test_asymp(lambda z: +phi, maxdps=10000)
test_asymp(lambda z: +catalan, maxdps=5000)
test_asymp(lambda z: +euler, maxdps=5000)
test_asymp(lambda z: +glaisher, maxdps=1000)
test_asymp(lambda z: +khinchin, maxdps=1000)
test_asymp(lambda z: +twinprime, maxdps=150)
test_asymp(lambda z: stieltjes(2), maxdps=150)
test_asymp(lambda z: +mertens, maxdps=150)
test_asymp(lambda z: +apery, maxdps=5000)
test_asymp(sqrt, maxdps=10000, huge_range=True)
test_asymp(cbrt, maxdps=5000, huge_range=True)
test_asymp(lambda z: root(z,4), maxdps=5000, huge_range=True)
test_asymp(lambda z: root(z,-5), maxdps=5000, huge_range=True)
test_asymp(exp, maxdps=5000, huge_range=True)
test_asymp(expm1, maxdps=1500)
test_asymp(ln, maxdps=5000, huge_range=True)
test_asymp(cosh, maxdps=5000)
test_asymp(sinh, maxdps=5000)
test_asymp(tanh, maxdps=1500)
test_asymp(sin, maxdps=5000, huge_range=True)
test_asymp(cos, maxdps=5000, huge_range=True)
test_asymp(tan, maxdps=1500)
test_asymp(agm, maxdps=1500, huge_range=True)
test_asymp(ellipk, maxdps=1500)
test_asymp(ellipe, maxdps=1500)
test_asymp(lambertw, huge_range=True)
test_asymp(lambda z: lambertw(z,-1))
test_asymp(lambda z: lambertw(z,1))
test_asymp(lambda z: lambertw(z,4))
test_asymp(gamma)
test_asymp(loggamma)  # huge_range=True ?
test_asymp(ei)
test_asymp(e1)
test_asymp(li, huge_range=True)
test_asymp(ci)
test_asymp(si)
test_asymp(chi)
test_asymp(shi)
test_asymp(erf)
test_asymp(erfc)
test_asymp(erfi)
test_asymp(lambda z: besselj(2, z))
test_asymp(lambda z: bessely(2, z))
test_asymp(lambda z: besseli(2, z))
test_asymp(lambda z: besselk(2, z))
test_asymp(lambda z: besselj(-2.25, z))
test_asymp(lambda z: bessely(-2.25, z))
test_asymp(lambda z: besseli(-2.25, z))
test_asymp(lambda z: besselk(-2.25, z))
test_asymp(airyai)
test_asymp(airybi)
test_asymp(lambda z: hyp0f1(a1, z))
test_asymp(lambda z: hyp1f1(a1, a2, z))
test_asymp(lambda z: hyp1f2(a1, a2, a3, z))
test_asymp(lambda z: hyp2f0(a1, a2, z))
test_asymp(lambda z: hyperu(a1, a2, z))
test_asymp(lambda z: hyp2f1(a1, a2, a3, z))
test_asymp(lambda z: hyp2f2(a1, a2, a3, a4, z))
test_asymp(lambda z: hyp2f3(a1, a2, a3, a4, a5, z))
test_asymp(lambda z: coulombf(a1, a2, z))
test_asymp(lambda z: coulombg(a1, a2, z))
test_asymp(lambda z: polylog(2,z))
test_asymp(lambda z: polylog(3,z))
test_asymp(lambda z: polylog(-2,z))
test_asymp(lambda z: expint(4, z))
test_asymp(lambda z: expint(-4, z))
test_asymp(lambda z: expint(2.25, z))
test_asymp(lambda z: gammainc(2.5, z, 5))
test_asymp(lambda z: gammainc(2.5, 5, z))
test_asymp(lambda z: hermite(3, z))
test_asymp(lambda z: hermite(2.5, z))
test_asymp(lambda z: legendre(3, z))
test_asymp(lambda z: legendre(4, z))
test_asymp(lambda z: legendre(2.5, z))
test_asymp(lambda z: legenp(a1, a2, z))
test_asymp(lambda z: legenq(a1, a2, z), maxdps=90)   # abnormally slow
test_asymp(lambda z: jtheta(1, z, 0.5))
test_asymp(lambda z: jtheta(2, z, 0.5))
test_asymp(lambda z: jtheta(3, z, 0.5))
test_asymp(lambda z: jtheta(4, z, 0.5))
test_asymp(lambda z: jtheta(1, z, 0.5, 1))
test_asymp(lambda z: jtheta(2, z, 0.5, 1))
test_asymp(lambda z: jtheta(3, z, 0.5, 1))
test_asymp(lambda z: jtheta(4, z, 0.5, 1))
test_asymp(barnesg, maxdps=90)
c                 C   sL   t | v r$t| ƒ tƒ }t| tƒ tƒ ƒ tƒ }|| }td|d| dƒ d S d S )NzTime:Úforz(OK))Úfiltr   Úclockr   ÚglobalsÚlocals)ÚlineÚt1Út2Úelapsedr   r   r   ÚtestitË   s   úrK   Ú__main__)ÚPoolz"Running tests with multiprocessingzNot using multiprocessingzCumulative wall time:)r	   FF)"Ú__doc__r'   ÚosÚtimeitr   rD   ÚargvÚremoveÚenvironrC   ÚendswithÚmpmathÚmpmath.libmp.backendr   r<   Úa1Úa2Úa3Úa4Úa5rA   ÚcasesrK   Ú__name__ÚmultiprocessingrM   ÚmapÚmapfr   ÚImportErrorrH   Ú
splitlinesÚtasksrI   r   r   r   r   Ú<module>   s>    (



+
]	
þ
ô