a
    ˜¥»aM8  ã                   @   sP  d dl Z 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	 zd dl
Z
W n ey^   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)Ú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   úT/home/yuhw/wc/pochoir/venv/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)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>ª   ó    zget_args.<locals>.<listcomp>r   c                 S   s   g | ]\}}|  |¡‘qS r   )r    )r?   r@   r1   r   r   r   rA   ­   rB   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    
 rM   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   rA   Ñ   rB   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   rC   Ú
issubdtypeÚdtypeÚcomplexfloatingÚ
is_complexÚanyÚdistinguish_nan_and_infÚgetattrÚnameÚparam_filter)r   rV   rW   rX   rf   rY   rZ   r   r[   r\   r]   rd   r^   rg   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Úmpcr4   ©r2   r   r   r   Úmptypeñ   s    z MpmathData.check.<locals>.mptypec                 S   s   t  t| ƒ¡S r&   )ri   Zmpfr	   rk   r   r   r   rl   ô   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Ú	mpf2floatrk   r   r   r   Úpytype÷   s    z MpmathData.check.<locals>.pytypec                     s   ˆˆj tˆ | ƒŽ ƒS r&   )rW   Úmap)r   ©rl   rp   r   r   r   Ú<lambda>  rB   z"MpmathData.check.<locals>.<lambda>F)Z
vectorizedr[   r\   r]   rd   r^   rg   r   )r   ÚrandomÚseedrM   rX   r   ri   ÚmprY   rZ   r_   r`   ra   Úmpc2complexÚ	enumerater   rV   r[   r\   r]   rd   r^   rg   ÚAssertionErrorrE   ÚsysÚexc_infoÚ__traceback__Úwith_traceback)
r   ZargarrZold_dpsZold_precZdps_listÚjrY   ÚtpÚvalueÚtbr   rr   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>)rb   rf   )r   r   r   r   Ú__repr__  s    zMpmathData.__repr__)
NNNNrO   rP   FTTN)r!   r"   r#   r   r‚   rƒ   r   r   r   r   rN   ³   s       ý
';rN   c                  O   s   t | i |¤Ž}| ¡  d S r&   )rN   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	   ri   Únstrrk   r   r   r   ro   *  s    	ro   c                 C   s   t t| jƒt| jƒƒS r&   )r4   ro   r+   r,   rk   r   r   r   rw   6  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>   ri   rj   r4   r	   rk   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Ú
)rz   ÚstderrÚwriteÚtuplerq   Úflush)r   r„   Úr©rŒ   r‘   r   r   ÚwrapA  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�   V  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_handlerj  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™   n  s     ûýÿú(time_limited.<locals>.deco.<locals>.wrapr   rš   ©r¤   r    r¥   r‹   r   Údecom  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Ÿ   ÚeventrR   )Ú
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¨   z  s    )ÚPOSIX)r¥   r¤   Zuse_sigalrmr¨   r   r§   r   Útime_limitedZ  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   rn   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   Úisfinitern   )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   sz  zt | ƒ W n ty&   t| ƒ} Y n0 t |ƒ}t | ƒ|krDtdƒ‚g }t|ƒD ]V}z4tt | | ||  ¡||t || ¡  kƒ W qP ty¤   | |¡ Y qP0 qPt	t
t |¡ƒƒ}dg}| d t |ƒ|¡¡ |D ]„}tj| | |ddd�}	tj|| |ddd�}
|| dk�rd}n*t | | ||  ||  ¡}t |d¡}| d ||	|
|¡¡ qÚ|�rvt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.

    zLengths of inputs not equal.Ú z4Bad results ({} out of {}) for the following points:r   r�   r   é   z{}: {} != {} (rdiff {})Fr’   N)rE   Ú	TypeErrorÚlistry   Úranger   ri   ÚfabsÚappendr.   rm   r   r   Úformatr�   Újoin)ÚresZstdr\   r[   r   ZfailuresÚkÚndigitsÚmsgZresrepZstdrepZrdiffr   r   r   Úmp_assert_allclose¢  s8    4ÿrÃ   )r   rµ   )"rS   rz   r©   Únumpyr   Znumpy.testingr   rˆ   Zscipy.special._testutilsr   ri   ÚImportErrorr   r%   r*   r5   rM   rN   r†   r�   ro   rw   r›   Úposixr¡   Údirr¯   r±   r�   rn   r°   r²   r´   rÃ   r   r   r   r   Ú<module>   s@   oj
4

