a
    ¬ˆcÓ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yj   Y n0 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ƒv ZW n e�y   dZY n0 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   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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   Ú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)r7   r8   r*   r   r   r   r   r6   ™   s   
r6   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>«   ó    zget_args.<locals>.<listcomp>r   c                 S   s   g | ]\}}|  |¡‘qS r   )r!   )r@   rA   r2   r   r   r   rB   ®   rC   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    
 rN   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 u rBzt tj dd¡ƒ}W n ty4   d}Y n0 |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   rB   Ò   rC   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   rD   Ú
issubdtypeÚdtypeÚcomplexfloatingÚ
is_complexÚanyÚdistinguish_nan_and_infÚgetattrÚnameÚparam_filter)r   rW   rX   rY   rg   rZ   r[   r   r\   r]   r^   re   r_   rh   Zis_xslowr   r   r   r   µ   s6    
zMpmathData.__init__c           
         sf  t j d¡ tˆjˆjƒ}tjjtjj	 }}�zˆjd urDˆjg}ndg}ˆj	d u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  �q8W q” t�y4   |t|ƒd k�r0t ¡ \}}}	|j|	u�r,| |	¡‚|‚Y q”0 q”W || tj_tj_	n|| tj_tj_	0 d S )NiÒ  é   c                 S   s   t  t| ƒ¡S r'   )ÚmpmathÚmpcr5   ©r3   r   r   r   Úmptypeò   s    z MpmathData.check.<locals>.mptypec                 S   s   t  t| ƒ¡S r'   )rj   Zmpfr
   rl   r   r   r   rm   õ   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Ú	mpf2floatrl   r   r   r   Úpytypeø   s    z MpmathData.check.<locals>.pytypec                     s   ˆˆj tˆ | ƒŽ ƒS r'   )rX   Úmap)r   ©rm   rq   r   r   r   Ú<lambda>  rC   z"MpmathData.check.<locals>.<lambda>F)Z
vectorizedr\   r]   r^   re   r_   rh   r   )r   ÚrandomÚseedrN   rY   r   rj   ÚmprZ   r[   r`   ra   rb   Úmpc2complexÚ	enumerater   rW   r\   r]   r^   re   r_   rh   ÚAssertionErrorrF   ÚsysÚexc_infoÚ__traceback__Úwith_traceback)
r   ZargarrZold_dpsZold_precZdps_listÚjrZ   ÚtpÚvalueÚtbr   rs   r   ÚcheckÜ   sF    




ø	
zMpmathData.checkc                 C   s"   | j rd| jf S d| jf S d S )Nz<MpmathData: %s (complex)>z<MpmathData: %s>)rc   rg   )r   r   r   r   Ú__repr__  s    zMpmathData.__repr__)
NNNNrP   rQ   FTTN)r"   r#   r$   r   rƒ   r„   r   r   r   r   rO   ´   s       ý
';rO   c                  O   s   t | i |¤Ž}| ¡  d S r'   )rO   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
   rj   Únstrrl   r   r   r   rp   +  s    	rp   c                 C   s   t t| jƒt| jƒƒS r'   )r5   rp   r,   r-   rl   r   r   r   rx   7  s    rx   c                    s   dd„ ‰‡ ‡fdd„}|S )Nc                 S   s    t | tjƒrt| ƒS t| ƒS d S r'   )r?   rj   rk   r5   r
   rl   r   r   r   Útofloat<  s    ztrace_args.<locals>.tofloatc               	      s|   t j dttˆ| ƒƒf ¡ t j ¡  z8ˆ | i |¤Ž}t j d| ¡ W t j d¡ t j ¡  nt j d¡ t j ¡  0 |S )Nz%r: z-> %rÚ
)r{   ÚstderrÚwriteÚtuplerr   Ú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               
      sÄ   t   t jˆ¡}t  t jˆ¡ z„z.ˆ | i |¤ŽW W t  t jd¡ t   t j|¡ S  ty€   ˆ Y W t  t jd¡ t   t j|¡ S 0 W t  t jd¡ t   t j|¡ nt  t jd¡ t   t j|¡ 0 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               	      sŒ   t   ¡ ‰ ‡ ‡‡fdd„‰t ˆ¡ zXzˆ| i |¤ŽW W t d ¡ S  tyl   t d ¡ ˆ Y W t d ¡ S 0 W t d ¡ nt d ¡ 0 d S )Nc                    s   t   ¡ ˆ  ˆkrtƒ ‚ˆS r'   )Útimerž   )r    ÚeventrS   )Ú
start_timer¦   Útracer   r   r­     s    z7time_limited.<locals>.deco.<locals>.wrap.<locals>.trace)rª   r{   Ú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ˆ | i |¤ŽW S  t y(   tj Y S 0 d S r'   )Ú	Exceptionr   ro   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"   ˆ | i |¤Ž}t  |¡st jS |S r'   )r   Úisfinitero   )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u s.|du 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“   )ry   r   r   rj   ÚfabsÚappendrF   r/   rn   r   r   Úformatr‘   r   Újoin)ÚresZstdr]   r\   ZfailuresÚkZresvalZstdvalZnfailÚndigitsÚmsgZresrepZstdrepZrdiffr   r   r   Úmp_assert_allclose£  s0     
ÿÿrÁ   )r   r¶   )$rT   r{   rª   Ú	itertoolsr   Únumpyr   Znumpy.testingr   r‰   Zscipy.special._testutilsr   rj   ÚImportErrorr   r&   r+   r6   rN   rO   r‡   rŽ   rp   rx   rœ   Úposixr¢   Údirr°   r²   rž   ro   r±   r³   rµ   rÁ   r   r   r   r   Ú<module>   sB   oj
4

