U
    »mœdÓ8  ã                   @   s`  d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dl	Z	d dl
mZ zd dlZW n ek
rl   Y nX G dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZG dd„ dƒZdd„ ZG dd„ dƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zz d dlZd dlZdeeƒkZW n ek
�r   dZY nX G dd„ deƒZdejd fd!d"„Z d#d$„ Z!d%d&„ Z"d*d(d)„Z#dS )+é    N)Úzip_longest)Úassert_)Úassert_func_equalc                   @   s8   e Zd ZdZej ejddfdd„Zdd„ Zdd„ Zd	S )
ÚArgz‚Generate a set of numbers on the real axis, concentrating on
    'interesting' regions and covering all orders of magnitude.

    Tc                 C   sf   ||krt dƒ‚|tj kr,dt t¡j }|tjkrFdt t¡j }|| | _| _|| | _| _	d S )Nz#a should be less than or equal to bg      à¿ç      à?)
Ú
ValueErrorÚnpÚinfZfinfoÚfloatÚmaxÚaÚbÚinclusive_aÚinclusive_b)Úselfr   r   r   r   © r   úS/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scipy/special/_mptestutils.pyÚ__init__   s    
zArg.__init__c                 C   sÀ  |dk rt dƒ‚|d dkr*|d }|}n|d }|d }|dkr`t t |¡t |¡|¡}�nV|dkr‚|dk r‚t |||¡}�n4|dkrÀtj|d|dd�}t dt |¡|¡}t ||f¡}nö|dk�r"|dk�r"t d||¡}|jdkrüt |d ¡}	nd}	tjd|	|dd�}t ||f¡}n”|d dk�r>|d }
|
}n|d }
|
d }tjdd|dd�}|jdk�r|t |d ¡}	nd}	tjd|	|
dd�}t dt |¡|¡}t |||f¡}t |¡S )	Nr   za should be positiveé   é   é
   F)Zendpointiâÿÿÿ)r   r   ZlogspaceÚlog10ZlinspaceÚhstackÚsizeÚsort)r   r   r   ÚnZnlogptsZnlinptsÚptsZlinptsZlogptsÚrightZnlogpts1Znlogpts2Zlogpts1Zlogpts2r   r   r   Ú_positive_values'   sF    
zArg._positive_valuesc           	      C   s  | j | j }}||kr t |¡S | js.|d7 }| js<|d7 }|d dkrV|d }|}n|d }|d }|dkr‚|  |||¡}g }nR|dkr¤g }|  | | |¡ }n0|  d||¡}|  d| |d ¡ }|dd… }t |ddd… |f¡}| j�s |dd… }| j�s|dd… }|S )z%Return an array containing n numbers.r   r   r   Néÿÿÿÿ)r   r   r   Zzerosr   r   r   r   )	r   r   r   r   Zn1Zn2ZposptsZnegptsr   r   r   r   Úvalues_   s6    
z
Arg.valuesN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r   r   r    r   r   r   r   r      s   8r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚFixedArgc                 C   s   t  |¡| _d S ©N)r   ÚasarrayÚ_values)r   r    r   r   r   r   †   s    zFixedArg.__init__c                 C   s   | j S r&   )r(   )r   r   r   r   r   r    ‰   s    zFixedArg.valuesN©r!   r"   r#   r   r    r   r   r   r   r%   …   s   r%   c                   @   s:   e Zd Zeej ej ƒeejejƒfdd„Zdd„ ZdS )Ú
ComplexArgc                 C   s$   t |j|jƒ| _t |j|jƒ| _d S r&   )r   ÚrealÚimag©r   r   r   r   r   r   r   Ž   s    zComplexArg.__init__c                 C   sX   t t t |¡¡ƒ}| j |¡}| j |d ¡}|d d …d f d|d d d …f    ¡ S )Nr   y              ð?)Úintr   ÚfloorÚsqrtr+   r    r,   Zravel)r   r   ÚmÚxÚyr   r   r   r    ’   s    zComplexArg.valuesN)r!   r"   r#   Úcomplexr   r	   r   r    r   r   r   r   r*   �   s   &r*   c                   @   s   e Zd Zddd„Zdd„ ZdS )	ÚIntArgéüÿÿéè  c                 C   s   || _ || _d S r&   )r   r   r-   r   r   r   r   š   s    zIntArg.__init__c                 C   sh   t | j| jƒ td|d  |d ƒ¡ t¡}t dd¡}t 	tj
||f ¡}||| jk|| jk @  }|S )Nr   r   é   éûÿÿÿ)r   r   r   r    r   Úastyper.   r   ZarangeÚuniqueZr_)r   r   Zv1Zv2Úvr   r   r   r    ž   s
    ,zIntArg.valuesN)r6   r7   r)   r   r   r   r   r5   ™   s   
r5   c                 C   s„   t | tjƒr|  ¡ }njt| ƒ}t dd„ | D ƒ¡}||t|ƒ   t¡d }dd„ t	| |ƒD ƒ}t 
tjtj|Ž Ž ¡ |d¡j}|S )Nc                 S   s   g | ]}t |tƒrd nd‘qS )g      ø?g      ð?©Ú
isinstancer*   )Ú.0Úspecr   r   r   Ú
<listcomp>«   s     zget_args.<locals>.<listcomp>r   c                 S   s   g | ]\}}|  |¡‘qS r   )r    )r?   r@   r1   r   r   r   rA   ®   s     r   )r>   r   ÚndarrayÚcopyÚlenr'   Úsumr:   r.   ÚzipÚarrayZbroadcast_arraysZix_ZreshapeÚT)Zargspecr   ÚargsÚnargsÚmsr   r   r   Úget_args¦   s    
 rL   c                
   @   s&   e Zd Zddd„Zdd	„ Zd
d„ ZdS )Ú
MpmathDataNçH¯¼šò×z>çYóøÂn¥FTc                 C   s  |d krDzt tj dd¡ƒ}W n tk
r6   d}Y nX |r@dnd}|| _|| _|| _|| _|| _	|| _
|| _|	| _|
| _|| _t| jtjƒr¤t | jjtj¡| _ntdd„ | jD ƒƒ| _|
| _|| _|rÒ|dkrÞt|d	d ƒ}|rê|dkröt|d	d ƒ}|| _|| _d S )
NZSCIPY_XSLOWÚ0Fiˆ  iô  c                 S   s   g | ]}t |tƒ‘qS r   r=   )r?   Úargr   r   r   rA   Ò   s     z'MpmathData.__init__.<locals>.<listcomp>z<lambda>r!   )r.   ÚosÚenvironÚgetr   Ú
scipy_funcÚmpmath_funcÚarg_specÚdpsÚprecr   ÚrtolÚatolÚignore_inf_signÚnan_okr>   r   rB   Ú
issubdtypeÚdtypeÚcomplexfloatingÚ
is_complexÚanyÚdistinguish_nan_and_infÚgetattrÚnameÚparam_filter)r   rU   rV   rW   re   rX   rY   r   rZ   r[   r\   rc   r]   rf   Zis_xslowr   r   r   r   µ   s6    
zMpmathData.__init__c           
         sV  t j d¡ tˆjˆjƒ}tjjtjj	 }}�zˆjd k	rDˆjg}ndg}ˆj	d k	r^ˆj	tj_	t  
|jt j¡r|t‰dd„ ‰ ndd„ ‰ dd„ ‰t|ƒD ]¤\}}|tj_zBtˆj‡ ‡‡fdd	„|d
ˆjˆjˆjˆjˆjˆjd�
 W  �q:W q” tk
�r6   |t|ƒd k�r2t ¡ \}}}	|j|	k	�r.| |	¡‚|‚Y q”X q”W 5 || tj_tj_	X d S )NiÒ  é   c                 S   s   t  t| ƒ¡S r&   )ÚmpmathÚmpcr4   ©r2   r   r   r   Úmptypeò   s    z MpmathData.check.<locals>.mptypec                 S   s   t  t| ƒ¡S r&   )rh   Zmpfr
   rj   r   r   r   rk   õ   s    c                 S   s0   t | jƒddt | jƒ  kr"tjS t| jƒS d S )Ng¼‰Ø—²Òœ<r   )Úabsr,   r+   r   ÚnanÚ	mpf2floatrj   r   r   r   Úpytypeø   s    z MpmathData.check.<locals>.pytypec                     s   ˆˆj tˆ | ƒŽ ƒS r&   )rV   Úmap)r   ©rk   ro   r   r   r   Ú<lambda>  ó    z"MpmathData.check.<locals>.<lambda>F)Z
vectorizedrZ   r[   r\   rc   r]   rf   r   )r   ÚrandomÚseedrL   rW   r   rh   ÚmprX   rY   r^   r_   r`   Úmpc2complexÚ	enumerater   rU   rZ   r[   r\   rc   r]   rf   ÚAssertionErrorrD   ÚsysÚexc_infoÚ__traceback__Úwith_traceback)
r   ZargarrZold_dpsZold_precZdps_listÚjrX   ÚtpÚvalueÚtbr   rq   r   ÚcheckÜ   sH    




 ø	
zMpmathData.checkc                 C   s"   | j rd| jf S d| jf S d S )Nz<MpmathData: %s (complex)>z<MpmathData: %s>)ra   re   )r   r   r   r   Ú__repr__  s    zMpmathData.__repr__)
NNNNrN   rO   FTTN)r!   r"   r#   r   r‚   rƒ   r   r   r   r   rM   ´   s                   ý
';rM   c                  O   s   t | |Ž}| ¡  d S r&   )rM   r‚   )r   ÚkwÚdr   r   r   Úassert_mpmath_equal  s    
r†   c                 C   s   t jjdd�| ƒS )Nz8    Test not yet functional (too slow), needs more work.)Úreason)ÚpytestÚmarkÚskip©Úfuncr   r   r   Únonfunctional_tooslow#  s    r�   c                 C   s   t tj| dddd�ƒS )zá
    Convert an mpf to the nearest floating point number. Just using
    float directly doesn't work because of results like this:

    with mp.workdps(50):
        float(mpf("0.99999999999999999")) = 0.9999999999999999

    é   r   ©Z	min_fixedZ	max_fixed)r
   rh   Únstrrj   r   r   r   rn   +  s    	rn   c                 C   s   t t| jƒt| jƒƒS r&   )r4   rn   r+   r,   rj   r   r   r   rw   7  s    rw   c                    s   dd„ ‰‡ ‡fdd„}|S )Nc                 S   s    t | tjƒrt| ƒS t| ƒS d S r&   )r>   rh   ri   r4   r
   rj   r   r   r   Útofloat<  s    ztrace_args.<locals>.tofloatc               	      sb   t j dttˆ| ƒƒf ¡ t j ¡  zˆ | |Ž}t j d| ¡ W 5 t j d¡ t j ¡  X |S )Nz%r: Ú
z-> %r)rz   ÚstderrÚwriteÚtuplerp   Úflush)r   r„   Úr©rŒ   r‘   r   r   ÚwrapB  s    

ztrace_args.<locals>.wrapr   ©rŒ   r™   r   r˜   r   Ú
trace_args;  s    
r›   Ú	setitimerFc                   @   s   e Zd ZdS )ÚTimeoutErrorN)r!   r"   r#   r   r   r   r   r�   W  s   r�   r   Tc                    s4   t r"|r"dd„ ‰‡ ‡‡fdd„}n‡ ‡fdd„}|S )a  
    Decorator for setting a timeout for pure-Python functions.

    If the function does not return within `timeout` seconds, the
    value `return_val` is returned instead.

    On POSIX this uses SIGALRM by default. On non-POSIX, settrace is
    used. Do not use this with threads: the SIGALRM implementation
    does probably not work well. The settrace implementation only
    traces the current thread.

    The settrace implementation slows down execution speed. Slowdown
    by a factor around 10 is probably typical.
    c                 S   s
   t ƒ ‚d S r&   )r�   )ÚsignumÚframer   r   r   Úsigalrm_handlerk  s    z%time_limited.<locals>.sigalrm_handlerc                    s   ‡ ‡‡‡fdd„}|S )Nc               
      sr   t   t jˆ¡}t  t jˆ¡ z2zˆ | |ŽW W ¢"S  tk
rJ   ˆ Y W ¢S X W 5 t  t jd¡ t   t j|¡ X d S )Nr   )ÚsignalÚSIGALRMrœ   ÚITIMER_REALr�   )r   r„   Zold_handler)rŒ   Ú
return_valr    Útimeoutr   r   r™   o  s    ú(time_limited.<locals>.deco.<locals>.wrapr   rš   ©r¤   r    r¥   r‹   r   Údecon  s    
ztime_limited.<locals>.decoc                    s   ‡ ‡‡fdd„}|S )Nc               	      sp   t   ¡ ‰ ‡ ‡‡fdd„‰t ˆ¡ z<zˆ| |ŽW W ¢,S  tk
rZ   t d ¡ ˆ Y W ¢S X W 5 t d ¡ X d S )Nc                    s   t   ¡ ˆ  ˆkrtƒ ‚ˆS r&   )Útimer�   )rŸ   ÚeventrQ   )Ú
start_timer¥   Útracer   r   r¬     s    z7time_limited.<locals>.deco.<locals>.wrap.<locals>.trace)r©   rz   Úsettracer�   ©r   r„   )rŒ   r¤   r¥   )r«   r¬   r   r™   |  s    

r¦   r   rš   )r¤   r¥   r‹   r   r¨   {  s    )ÚPOSIX)r¥   r¤   Zuse_sigalrmr¨   r   r§   r   Útime_limited[  s
    r°   c                    s   ‡ fdd„}|S )z9Decorate function to return nan if it raises an exceptionc                     s,   zˆ | |ŽW S  t k
r&   tj Y S X d S r&   )Ú	Exceptionr   rm   r®   r‹   r   r   r™   ‘  s    zexception_to_nan.<locals>.wrapr   rš   r   r‹   r   Úexception_to_nan�  s    r²   c                    s   ‡ fdd„}|S )z1Decorate function to return nan if it returns infc                     s   ˆ | |Ž}t  |¡st jS |S r&   )r   Úisfiniterm   )r   r„   r<   r‹   r   r   r™   ›  s    

zinf_to_nan.<locals>.wrapr   rš   r   r‹   r   Ú
inf_to_nan™  s    r´   ç—ÔFFõg<c              	   C   s2  g }t t| |ƒƒD ]T\}\}}|dks.|dkr6tdƒ‚t || ¡||t |¡  kr| |||f¡ qt|ƒ}|dk�r.ttt	 
|¡ƒƒ}	dg}
|
 d ||d ¡¡ |D ]p\}}}tj||	ddd�}tj||	ddd�}|dkrèd}nt || | ¡}t |d	¡}|
 d
 ||||¡¡ q¬tdd |
¡ƒ dS )z|
    Compare lists of mpmath.mpf's or mpmath.mpc's directly so that it
    can be done to higher precision than double.
    Nz,Lengths of inputs res and std are not equal.r   Ú z4Bad results ({} out of {}) for the following points:r   r�   r	   é   z{}: {} != {} (rdiff {})Fr’   )rx   r   r   rh   ÚfabsÚappendrD   r.   rl   r   r   Úformatr�   r   Újoin)ÚresZstdr[   rZ   ZfailuresÚkZresvalZstdvalZnfailÚndigitsÚmsgZresrepZstdrepZrdiffr   r   r   Úmp_assert_allclose£  s2     
 ÿÿrÀ   )r   rµ   )$rR   rz   r©   Ú	itertoolsr   Únumpyr   Znumpy.testingr   rˆ   Zscipy.special._testutilsr   rh   ÚImportErrorr   r%   r*   r5   rL   rM   r†   r�   rn   rw   r›   Úposixr¡   Údirr¯   r±   r�   rm   r°   r²   r´   rÀ   r   r   r   r   Ú<module>   sB   oj
4

