3
5^n/              	   @   s   d dl mZmZm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 d dlZd dljZdddgZG dd	 d	eZd
d Zdd ZdddZG dd deZdS )    )divisionprint_functionabsolute_importN)LooseVersion)assert_with_special_errorsassert_func_equalFuncDatac               @   s   e Zd Zdd ZdS )MissingModulec             C   s
   || _ d S )N)name)selfr    r   7lib/python3.6/site-packages/scipy/special/_testutils.py__init__   s    zMissingModule.__init__N)__name__
__module____qualname__r   r   r   r   r   r
      s   r
   c             C   sJ   t | tkr"tjjdj| jdS tjjt| j	t|k dj| j
|dS )Nz{} is not installed)reasonz{} version >= {} required)typer
   pytestZmarkskipformatr   Zskipifr   __version__r   )moduleZmin_verr   r   r   check_version   s    r   c                s   t j  fdd}|S )zc
    Enable special function errors (such as underflow, overflow,
    loss of precision, etc.)
    c           
      s&   t jdd  | |}W d Q R X |S )Nraise)all)scZerrstate)akwres)funcr   r   wrapper*   s    z$with_special_errors.<locals>.wrapper)	functoolswraps)r!   r"   r   )r!   r   r   %   s    TFc             C   s   t |drt|}tj|}|jdkr6|d d d f }|jd }t |drX|}d }|}n&tj||f }tt||jd }d }t| |tt||||||||	||
|d}|j	  d S )Nnext   r   )
result_columnsresult_funcrtolatolparam_filterknownfailurenan_ok
vectorizedignore_inf_signdistinguish_nan_and_inf)
hasattrlistnpasarrayndimshapeZc_ranger	   check)r!   ZresultsZpointsr)   r*   r+   r,   r.   dtyper-   r/   r0   Znparamsdatar'   r(   Zfdatar   r   r   r   7   s(    




c               @   s4   e Zd ZdZdddZdd Zdd	d
Zdd ZdS )r	   a  
    Data set for checking a special function.

    Parameters
    ----------
    func : function
        Function to test
    data : numpy array
        columnar data to use for testing
    param_columns : int or tuple of ints
        Columns indices in which the parameters to `func` lie.
        Can be imaginary integers to indicate that the parameter
        should be cast to complex.
    result_columns : int or tuple of ints, optional
        Column indices for expected results from `func`.
    result_func : callable, optional
        Function to call to obtain results.
    rtol : float, optional
        Required relative tolerance. Default is 5*eps.
    atol : float, optional
        Required absolute tolerance. Default is 5*tiny.
    param_filter : function, or tuple of functions/Nones, optional
        Filter functions to exclude some parameter ranges.
        If omitted, no filtering is done.
    knownfailure : str, optional
        Known failure error message to raise when the test is run.
        If omitted, no exception is raised.
    nan_ok : bool, optional
        If nan is always an accepted result.
    vectorized : bool, optional
        Whether all functions passed in are vectorized.
    ignore_inf_sign : bool, optional
        Whether to ignore signs of infinities.
        (Doesn't matter for complex-valued functions.)
    distinguish_nan_and_inf : bool, optional
        If True, treat numbers which contain nans or infs as as
        equal. Sets ignore_inf_sign to be True.

    NFTc             C   s   || _ || _|
| _t|ds"|f}t|| _|d k	r`t|dsD|f}t|| _|d k	rxtdn|d k	rpd | _ntd|| _|| _	|| _
t|ds|f}|| _|	| _|| _|| _|| _|| _| jsd| _d S )N__len__z5Only result_func or result_columns should be providedz7Either result_func or result_columns should be providedT)r!   r:   datanamer1   tupleparam_columnsr'   
ValueErrorr(   r)   r*   r+   r,   r-   r.   r/   r0   )r   r!   r:   r>   r'   r(   r)   r*   r+   r,   r<   r-   r.   r/   r0   r   r   r   r      s8    





zFuncData.__init__c             C   s\   t j|t jst jt}t j|}| j| j }}|d krBd|j }|d krTd|j	 }||fS )N   )
r3   
issubdtypeZinexactr9   floatZfinfor)   r*   ZepsZtiny)r   r9   infor)   r*   r   r   r   get_tolerances   s    



zFuncData.get_tolerancesc       *         s  t jdt}jr tjjd  dkr.j |dkr> j}n
 j| j	|\}}j
rtj jd ftj}x:tjj
D ](\}|r|t| ddf M }qW  |  g xtjD ]\}	tjrtjj ddf jt q|r8|	t|k r8j ddf j||	  qƈj ddf  qW dfdd	}
|
j}jdk	rt fddjD }n6d}jrt|d	krtj|d }|
j|d
}tt|t|k xtt||D ]\}\}}tj |jtj!sj"r@tj#|}tj#|}tj#|}tj#|}n(tj$|}tj$|}tj%|}tj%|}tj|}tj|}tj&dd}zZtj'|}d|tj(| < tj'|| }d|tj(| < |tj'| }d|tj(| < W dtj&f | X ||||  k}||k}||k}||k}||@ |@ |@  }|j)} jr^|| M }|| M }| ||B j* 8 } j+ rj rtj#|}!tj#|}"|!|@ ||"@ B }#||# M }| |#j* 8 } tj,|rdg}$|$jd|| j-   |$jd|| j-   |$jdtj*|| |f  xztj.|d D ]htfdd}%dj/t0|%}&dj/t0|%|}'dj/t0|%|}(|%|})|$jd|&|'|(|)f  qW tddj/|$ qW dS )z,Check the special function against the data.Zerrisinstance)r   Nr   c                s   j r|  }npg }x`ttd D ]L |d k	rH|  rH|jtj q&|j| t fddttD   q&W tj|}t|ts|f}|S )Nr   c                s   g | ]}|   qS r   r   ).0i)jparamsr   r   
<listcomp>   s    z?FuncData.check.<locals>.eval_func_at_params.<locals>.<listcomp>)	r.   r7   lenappendr3   nanr=   r4   
isinstance)r!   	skip_maskgot)rH   r   )rG   r   eval_func_at_params   s    
.

z+FuncData.check.<locals>.eval_func_at_paramsc                s   g | ]} d d |f qS )Nr   )rE   Zicol)r:   r   r   rI      s    z"FuncData.check.<locals>.<listcomp>r&   )rN   ignore)r    zMax |adiff|: %gzMax |rdiff|: %gzCBad results (%d out of %d) for the following points (in output %d):c                s   dt j|   dd S )Nz%30s   )Z	precision)r3   Zarray2string)x)rG   r   r   <lambda>/  s    z FuncData.check.<locals>.<lambda>z  z%s => %s != %s  (rdiff %s)F
)N)1operatormethodcallerAssertionErrorr,   r   Zxfailr:   r9   ZastyperD   r+   r3   Zonesr6   Zbool_zipr>   r2   	enumerateiscomplexobjintimagrK   complexrJ   r!   r'   r=   r-   Zisnanr(   r   rA   Zcomplexfloatingr/   ZisinfZisposinfZisneginfZseterrZabsoluteZisfinitesizesumr0   anymaxZnonzerojoinmap)*r   r:   r9   ZdtypesZ__tracebackhide__r)   r*   Z
param_maskfilteridxrP   rO   ZwantedrN   Z
output_numrT   yZpinf_xZpinf_yZminf_xZminf_yZnan_xZnan_yZolderrZabs_yZdiffZrdiffZtol_maskZ	pinf_maskZ	minf_maskZnan_maskZbad_jZpoint_countZinf_xZinf_yZboth_nonfinitemsgZfmtr   bcdr   )r:   rG   rH   r   r   r8      s    
 
"
 














zFuncData.checkc             C   sX   t jttt j| jrd}nd}| jrDd| jj|t	j
j| jf S d| jj|f S dS )z%Pretty-printing, esp. for Nose outputz
 (complex)rR   z<Data for %s%s: %s>z<Data for %s%s>N)r3   rb   r2   re   r\   r>   r<   r!   r   ospathbasename)r   Z
is_complexr   r   r   __repr__7  s    
zFuncData.__repr__)NNNNNNNFTFT)NNN)r   r   r   __doc__r   rD   r8   rp   r   r   r   r   r	   X   s   '   

 
)	NNNNTNFFT)Z
__future__r   r   r   rm   r#   rW   Zdistutils.versionr   Znumpyr3   Znumpy.testingr   r   Zscipy.specialZspecialr   __all__objectr
   r   r   r   r	   r   r   r   r   <module>   s"   

   
