a
    ߙfbb                     @   s  d dl Zd dlZd dlmZ d dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ ed
g dZedg dZedg dZejjdd Zejjdd Zejjdd Z G dd dZ!G dd dZ"G dd dZ#G dd dZ$G dd dZ%G d d! d!Z&ejj'e(ej)j*d" d#d$G d%d& d&Z+G d'd( d(Z,G d)d* d*Z-G d+d, d,Z.erd d-l/m0Z1 e1j2e1j3e1j4e1j5e1j6e1j7e1j8e1j9e1j:e1j;e1j<e1j=e1j>e1j?e1j@e1jAe1jBe1jCe1jDfZEeFe1jGejreEe1jGe1jHf7 ZEd.d/ ZIG d0d1 d1ZJdS )2    N)
namedtuple)assert_allclose)ufunc)units)quantity_helper)UfuncHelpershelper_sqrt)	HAS_SCIPYtestcasefq_inq_outtestexcr   r   excmsgtestwarnr   r   wfilterc                 C   s^   | j | j }t|tkr|fn|}t|| jD ]*\}}|j|jksFJ t|j|jdd q.d S )NgV瞯<)Zatol)	r   r   typetuplezipr   unitr   value)tcZresultsresultexpected r   Glib/python3.9/site-packages/astropy/units/tests/test_quantity_ufuncs.pytest_testcase   s
    r!   c                 C   s\   t | j}| j| j  W d    n1 s.0    Y  | jd urX| j|jjd v sXJ d S )Nr   )pytestraisesr   r   r   r   r   args)ter   r   r   r    test_testexc$   s    *
r&   c                 C   sD   t  ( t | j | j| j  W d    n1 s60    Y  d S N)warningscatch_warningsfilterwarningsr   r   r   )twr   r   r    test_testwarn,   s    
r,   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestUfuncHelpersc                 C   st   t dd tjjj D }tjt tj	 B }|| t  ksBJ t dd t
j D }|| | t  kspJ dS )zTest that we cover all ufunc'sc                 S   s   g | ]}t |tjr|qS r   
isinstancenpr   .0r   r   r   r    
<listcomp>;   s   z2TestUfuncHelpers.test_coverage.<locals>.<listcomp>c                 S   s   g | ]}t |tjr|qS r   r.   r1   r   r   r    r3   E   s   N)setr0   coreumath__dict__valuesqhUNSUPPORTED_UFUNCSUFUNC_HELPERSkeys
erfa_ufunc)selfZall_np_ufuncsZall_q_ufuncsZall_erfa_ufuncsr   r   r    test_coverage8   s    zTestUfuncHelpers.test_coveragec                 C   s   dt jjv sJ d S )Nzscipy.special)r9   r;   modulesr>   r   r   r    test_scipy_registeredI   s    z&TestUfuncHelpers.test_scipy_registeredc                 C   s   t jtjv sJ t jtjvs J d tjt j< t jtjvs<J t jtjv sLJ tjt j tjt j< t jtjv spJ t jtjvsJ d S r'   )r0   addr9   r;   r:   subtractrA   r   r   r    test_removal_additionM   s    z&TestUfuncHelpers.test_removal_additionc                    s   dd   fdd}d}t jj|dntdD ]T}t dd	g|  fd
dt|D }dd |D }|tg| ks0J q0W d    n1 s0    Y  d S )Nc                  _   s   t j| i |S r'   )r0   sqrt)r$   kwargsr   r   r    dummy_ufuncX   s    z8TestUfuncHelpers.test_thread_safety.<locals>.dummy_ufuncc                      s    t iS r'   r   r   )rH   r   r    register[   s    z5TestUfuncHelpers.test_thread_safety.<locals>.register   )Zmax_workersi'  z(astropy.units.tests.test_quantity_ufuncsrH   c                    s    g | ]}  fd dqS )c                      s     S r'   r   r   )rH   helpersr   r    <lambda>g       z@TestUfuncHelpers.test_thread_safety.<locals>.<listcomp>.<lambda>)Zsubmit)r2   irH   ZexecutorrK   r   r    r3   g   rM   z7TestUfuncHelpers.test_thread_safety.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )r   )r2   Zfuturer   r   r    r3   h   rM   )
concurrentfuturesZThreadPoolExecutorranger   Zregister_moduler	   )r>   Zfast_thread_switchingrI   ZworkersprQ   r8   r   rO   r    test_thread_safetyW   s    z#TestUfuncHelpers.test_thread_safetyN)__name__
__module____qualname__r?   rB   rE   rT   r   r   r   r    r-   3   s   
r-   c                   @   s  e Zd ZdZejdeej	de
j fde
j fdeej	edejd ejd ge
j fedd	ed d	ge
j fdeeje	de
j fede
j fdeeje	edejd ejd ge
j fedejd ejd ge
j fdeejejd
 e
j fde
j fdeejedejd ejd ge
j fed	d	ed dge
j fdeejeejd
 e
j fejd
 e
j fdeejeedejd ejd ge
j fedejd ejd ge
j fdeejejd
 e
j fed
e
j fdeejeg de
j feg de
j fdeejeejd
 e
j fejd
 e
j fdeejeeg de
j feeg de
j fdeejeg de
j de
j feeg dde
j fdeejeddge
j de
j  e
jd	feeddgd d	e
j fdeejde
j feje
j fdeejde
j feje
j fdeejd
e
j fd
e
j fdeejd
e
j fd
e
j fdeejde
j fde
j fdeejde
j fde
j fdeejeje
j fde
j fdeejeje
j fde
j fdfdd Zejde ejd
e
j fe!dde ejd
e
j fe!dde ejd
e
j e!dde ejd
e
j e!dde ej	d
e
j fe!dde ejd
e
j fe!dde ejd
e
j" fe!dde ejd
e
j" fe!dde ejeg de
j# fe!dde ejeg de
j# fe!dde ejeg de
j# d	e
j" fe
j$dde ejeg de
j# d	fe
j$d dfd!d" Z%ejd#e&ejd$e
j' d%e
j(  fd&d'fd(d) Z)dS )*TestQuantityTrigonometricFuncsz&
    Test trigonometric functions
    r         >@      ?r                 @       @      ?      @)r[   g     F@g     `@     f@)r[   r^         r[   )      $@rY        Q@      T@g     @@rb   rd   r`   g      N@c                 C   s   t |S r'   )r!   )r>   r   r   r   r    test_testcasesp   s    rz-TestQuantityTrigonometricFuncs.test_testcasesr%   Nr   z<Can only apply 'sin' function to quantities with angle unitsz<Can only apply 'arcsin' function to dimensionless quantitiesz<Can only apply 'cos' function to quantities with angle unitsz<Can only apply 'arccos' function to dimensionless quantities)         z<Can only apply 'tan' function to quantities with angle unitsz<Can only apply 'arctan' function to dimensionless quantitiescompatible dimensionsz'dimensionless quantities when other argc                 C   s   t |S r'   )r&   )r>   r%   r   r   r    test_testexcs   s    Kz,TestQuantityTrigonometricFuncs.test_testexcsr+         ;@   errorr   c                 C   s   t |S r'   )r,   )r>   r+   r   r   r    test_testwarns1  s    z-TestQuantityTrigonometricFuncs.test_testwarns)*rU   rV   rW   __doc__r"   markparametrizer   r0   sinudegreedimensionless_unscaledarraypiradianrF   ZonearcsinZradiansZcosZarccosZtanZarctanarctan2mkmtoZdeg2radZrad2degZdegreesre   r   	TypeErrorsN
UnitsErrorrj   r   ZpcZkpcrn   r   r   r   r    rX   l   s  

" ("
" ("( 











q






J
rX   c                
   @   s\  e Zd ZdZdd Zdd Zejje	e
eddej dd	d
d Zejdejejfdd Zejdejejfdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zejdejfd#d$ Zejdejfd%d& Zd'd( Zd)d* Z d+d, Z!d-d. Z"d/d0 Z#d1d2 Z$d3d4 Z%d5d6 Z&d7d8 Z'd9d: Z(d;d< Z)d=d> Z*ejdej+ej,ej-ej.ej/ej0ej1fd?d@ Z2ejdej+ej,ej-ej.ej/ej0ej1fdAdB Z3ejdej+ej,ej-ej.ej/ej0ej1fdCdD Z4dEdF Z5dGdH Z6dIdJ Z7dKdL Z8dMdN Z9ejdej:ej;fdOdP Z<ejdej:ej;fdQdR Z=dS )STestQuantityMathFuncsz+
    Test other mathematical functions
    c                 C   sp   t dtj dtj dtj tj ks,J t dtj ddtj ksLJ t ddtj dtj kslJ d S )Nr\   r]          @)r0   multiplyrs   r{   r   rA   r   r   r    test_multiply_scalarA  s    , z*TestQuantityMathFuncs.test_multiply_scalarc                 C   sF   t t t dtj dtj t dddtj tj ksBJ d S )Nr_   r]   r         @)r0   allr   arangers   r{   r   rA   r   r   r    test_multiply_arrayF  s     z)TestQuantityMathFuncs.test_multiply_arraymatmulNznp.matmul is not yet a gufuncreasonc                 C   s   t dtj }t ||}|dtjd  ks2J t dtj }t g dg dg dgg dg dg dgg dg dg dggtj }t ||}t |t |j	|j	|j
 |j
 ksJ d S )Nr_         @rg   rh   )r^   r[   r[   )r[   r^   r[   )r[   r[   r^   )r0   r   rs   r{   r   Zeyerv   r   r   r   r   )r>   qrq1q2Zr2r   r   r    test_matmulJ  s*    
	z!TestQuantityMathFuncs.test_matmulfunctionc                 C   s|   |dt j dt j |ddt j t j ks0J |dt j d|ddt j ksTJ |ddt j |ddt j ksxJ d S Nr\   r]   )rs   r{   r   r>   r   r   r   r    test_divide_scalar^  s    0$z(TestQuantityMathFuncs.test_divide_scalarc                 C   sF   t |t dtj dtj |t ddtj tj ksBJ d S )Nr_   r]   )r0   r   r   rs   r{   r   r   r   r   r    test_divide_arrayd  s    z'TestQuantityMathFuncs.test_divide_arrayc                 C   s  t dt j }tg dt j }tg d| }|| }|| }t|jg d |jt jksfJ t|jg d |j|jksJ t	||}t
||}t||ksJ t||ksJ t||\}}	t||ksJ t|	|ksJ tt t|t j W d    n1 s"0    Y  tt |t j  W d    n1 sX0    Y  tt |t j  W d    n1 s0    Y  t||\}
}t|
|ksJ t||ksJ tt t|t j W d    n1 s 0    Y  d S )Ng
F%u?r^   r]   r_   )r_   r\   r   )g      *@g      3@g      7@)gUN@?g5;Nѱ?gMbX9?)rs   ZUnitr{   r0   rv   r   r   r   ru   Zfloor_divide	remainderr   divmodr"   r#   r~   r|   )r>   ZinchZdividenddivisorZquotientr   Z	quotient2Z
remainder2Z	quotient3Z
remainder3Z	quotient4Z
remainder4r   r   r    &test_floor_divide_remainder_and_divmodi  s6    ,**z<TestQuantityMathFuncs.test_floor_divide_remainder_and_divmodc                 C   s&   t dtj dtjd  ks"J d S )Nr\   r]   rZ   )r0   rF   rs   r{   rA   r   r   r    test_sqrt_scalar  s    z&TestQuantityMathFuncs.test_sqrt_scalarc                 C   s@   t t t g dtj t g dtjd  ks<J d S )Nr^   r\         "@r   rZ   )r0   r   rF   rv   rs   r{   rA   r   r   r    test_sqrt_array  s    z%TestQuantityMathFuncs.test_sqrt_arrayc                 C   s&   t dtj dtjd  ks"J d S )Nr\         0@rg   )r0   squarers   r{   rA   r   r   r    test_square_scalar  s    z(TestQuantityMathFuncs.test_square_scalarc                 C   s@   t t t g dtj t g dtjd  ks<J d S )Nr   r   rg   )r0   r   r   rv   rs   r{   rA   r   r   r    test_square_array  s    z'TestQuantityMathFuncs.test_square_arrayc                 C   s"   t dtj dtj ksJ d S )Nr\         ?)r0   
reciprocalrs   r{   rA   r   r   r    test_reciprocal_scalar  s    z,TestQuantityMathFuncs.test_reciprocal_scalarc                 C   s<   t t t g dtj t g dtj ks8J d S )N)r^   r]   r\   )r^   rZ   r   )r0   r   r   rv   rs   r{   rA   r   r   r    test_reciprocal_array  s    z+TestQuantityMathFuncs.test_reciprocal_arrayc                 C   sj   t dtj ddtj ks J t dtj dtj dtj ksFJ t dtj ddtj ksfJ d S )Nr[   rZ      r   r]   r^   )r0   	heavisiders   r{   ru   r   percentJrA   r   r   r    test_heaviside_scalar  s     z+TestQuantityMathFuncs.test_heaviside_scalarc                 C   sV   t g d}t g dtj }t t |tj |tj g dtj ksRJ d S )N)ra   r[   r[   r^   )      ?r   r   r   )r   r   r   r^   )r0   rv   rs   ru   r   r   r{   )r>   r8   Zhalfwayr   r   r    test_heaviside_array  s    z*TestQuantityMathFuncs.test_heaviside_arrayc                 C   s$   |dt jd  dt j ks J d S )Nr   rh   r]   )rs   r{   r   r   r   r    test_cbrt_scalar  s    z&TestQuantityMathFuncs.test_cbrt_scalarc                 C   s<   t g d}t ||tjd  ||tj ks8J d S )N)r^   r   g      P@rh   )r0   rv   r   rs   r{   )r>   r   r8   r   r   r    test_cbrt_array  s    z%TestQuantityMathFuncs.test_cbrt_arrayc                 C   sr   t dtj ddtjd  ks$J t ddtj tj tdtjksNJ t dtj ddtj ksnJ d S )Nr\   r]   r   rg   g      i@r[   r^   )r0   powerrs   r{   cmQuantityru   rA   r   r   r    test_power_scalar  s
    $z'TestQuantityMathFuncs.test_power_scalarc                 C   sn   t t t g dtj dt g dtjd  ks>J t t t dtj ddtj ksjJ d S Nr   r_   )r^   r   rk   rh   r\   r[   r^   )r0   r   r   rv   rs   r{   r   ru   rA   r   r   r    test_power_array  s    
z&TestQuantityMathFuncs.test_power_arrayc                 C   sn   t t t g dtj dt g dtjd  ks>J t t t dtj ddtj ksjJ d S r   )r0   r   Zfloat_powerrv   rs   r{   r   ru   rA   r   r   r    test_float_power_array  s    
z,TestQuantityMathFuncs.test_float_power_arrayc                 C   sD   t t& tdtj ddg W d    n1 s60    Y  d S r   r"   r#   
ValueErrorr0   r   rs   r{   rA   r   r   r    test_power_array_array  s    z,TestQuantityMathFuncs.test_power_array_arrayc                 C   sH   t t* tddgtj ddg W d    n1 s:0    Y  d S )Nr]   r\   r   rA   r   r   r    test_power_array_array2  s    z-TestQuantityMathFuncs.test_power_array_array2c                 C   s  ddgt j t j }ddg}t||}t|j|j| ksBJ |jt jksRJ ddgt j t j }t	t
tjtjjt jfD ]B}t|||}t|j|d| ksJ |jt jks|J q||d }t|j|jd ksJ |j|jd ksJ t||ddg ksJ d S )Nr]   r\   rf   rg   )rs   r{   r0   r   r   r   r   ru   r   listr   rv   Zmar   to_value)r>   r   Zpowersresr   clsZres2Zres3r   r   r    test_power_array_array3  s    z-TestQuantityMathFuncs.test_power_array_array3c                 C   sT   t t"}tddtj  W d    n1 s20    Y  d|jjd v sPJ d S )Nr_   r\   z"raise something to a dimensionlessr   )	r"   r#   r~   r0   r   rs   r{   r   r$   r>   r   r   r   r    test_power_invalid  s    0z(TestQuantityMathFuncs.test_power_invalidc                 C   s   t dtj ddtj ks J t dtj dtj dtj ksFJ t dtj ddtj ksfJ t dtj dtj dtj ksJ d S )Nrh   r^   r_   ra         )r0   copysignrs   r{   r   rA   r   r   r    test_copysign_scalar  s     & z*TestQuantityMathFuncs.test_copysign_scalarc              	   C   s  t t t g dtj dt g d tj ks<J t t t g dtj dtj t g d tj ks~J t t t g dtj t g dtj t g dtj ksJ t t g ddtj }t |t g dksJ t|tjrJ d S )Nr   ra   )       r]   g      )ra   r]   r   )ra   r   r   )	r0   r   r   rv   rs   r   r{   r/   r   r>   r   r   r   r    test_copysign_array  s    
$

z)TestQuantityMathFuncs.test_copysign_arrayc                 C   s$   t dtj ddtj ks J d S )Nr\   rg   r   )r0   ldexprs   r{   rA   r   r   r    test_ldexp_scalar  s    z'TestQuantityMathFuncs.test_ldexp_scalarc                 C   sB   t t t g dtj g dt g dtj ks>J d S )Nr   )rh   rg   rf   )r   r   r   )r0   r   r   rv   rs   r{   rA   r   r   r    test_ldexp_array  s    "z&TestQuantityMathFuncs.test_ldexp_arrayc              	   C   s   t t" tdtj d W d    n1 s20    Y  t t* tdtjdtjtd W d    n1 sv0    Y  d S )Nr_   r\      dtype)	r"   r#   r~   r0   r   rs   r{   r   intrA   r   r   r    test_ldexp_invalid	  s    0z(TestQuantityMathFuncs.test_ldexp_invalidc                 C   s>   |dt j dt j  }|jt jks(J |j|dks:J d S )Nr_   r   rZ   rs   r{   r   ru   r   r>   r   r   r   r   r    test_exp_scalar  s    z%TestQuantityMathFuncs.test_exp_scalarc                 C   s   |t g dtj dtj  }|jtjks2J t |j|t g dksTJ |t g dtj dtj  }|jtjksJ t	|j|t g d d S )Nr]   r_   r   r   gUUUUUU?rZ   r^   g@@g      I@      Y@)
r0   rv   rs   r{   r   ru   r   r   r   r   r>   r   r   r   r   r   r    test_exp_array  s    "
"z$TestQuantityMathFuncs.test_exp_arrayc                 C   s^   t t$}|dtj tj  W d    n1 s40    Y  |jjd d|j	ksZJ d S )Nr_   r   8Can only apply '{}' function to dimensionless quantities)
r"   r#   r~   rs   r{   r   r   r$   formatrU   r>   r   r   r   r   r    test_exp_invalid_units$  s
    2z,TestQuantityMathFuncs.test_exp_invalid_unitsc                 C   s:   t dtj dtj  }|dtj dtj fks6J d S )Nr   g     @rZ   r^   )r0   modfrs   r{   r   ru   r   r   r   r    test_modf_scalar.  s    
z&TestQuantityMathFuncs.test_modf_scalarc                 C   s   t dtj dtj  }t |}t |tj}|d jtjksJJ |d jtjks^J t	|d j
|d ksxJ t	|d j
|d ksJ d S )Nrb   g     @@r   rf   )r0   r   rs   r{   r   r   r   ru   r   r   r   )r>   vr   nr   r   r    test_modf_array3  s    
z%TestQuantityMathFuncs.test_modf_arrayc                 C   s:   t dtj dtj  }|t dt dfks6J d S )Nr_   r   rZ   r[   )r0   frexprs   r{   rv   r   r   r   r    test_frexp_scalar<  s    z'TestQuantityMathFuncs.test_frexp_scalarc                 C   sR   t t g dtj dtj  }tdd t|d |d g dD sNJ d S )Nr   r   c                 s   s&   | ]\}}}||ft |kV  qd S r'   )r0   r   )r2   Z_q0Z_q1Z_dr   r   r    	<genexpr>B  rM   z9TestQuantityMathFuncs.test_frexp_array.<locals>.<genexpr>r   rf   r   )r0   r   rv   rs   r{   r   r   r   r   r   r    test_frexp_array@  s    $z&TestQuantityMathFuncs.test_frexp_arrayc                 C   s   t t&}tdtj tj  W d    n1 s60    Y  |jj	d dksTJ t t4}tt
g dtj dtj   W d    n1 s0    Y  |jj	d dksJ d S )Nr_   r   zDCan only apply 'frexp' function to unscaled dimensionless quantitiesr   r   )r"   r#   r~   r0   r   rs   r{   r   r   r$   rv   r   r   r   r   r    test_frexp_invalid_unitsE  s    4Bz.TestQuantityMathFuncs.test_frexp_invalid_unitsc                 C   sT   |t g dtj dtj  d}|jtjks4J t|j|t g dd d S )Nr   r   r^   r   )	r0   rv   rs   r{   r   r   ru   r   r   r   r   r   r    test_dimensionless_twoarg_arrayR  s
    $z5TestQuantityMathFuncs.test_dimensionless_twoarg_arrayc                 C   sl   t t2}|dtj tj dtj tj  W d    n1 sB0    Y  |jjd d	|j
kshJ d S Nr^   r_   r   r   )r"   r#   r~   rs   r|   r   r{   r   r$   r   rU   r   r   r   r    'test_dimensionless_twoarg_invalid_unitsY  s
    @z=TestQuantityMathFuncs.test_dimensionless_twoarg_invalid_units)>rU   rV   rW   ro   r   r   r"   rp   skipifr/   getattrr0   r   r   rq   ZdivideZtrue_divider   r   r   r   r   r   r   r   r   r   r   cbrtr   r   r   r   r   r   r   r   r   r   r   r   r   r   expexpm1exp2logZlog2Zlog10log1pr   r   r   r   r   r   r   r   Z	logaddexpZ
logaddexp2r   r   r   r   r   r    r   <  sr   


!




	
r   c                   @   s  e Zd Zejdejejej	ej
ejejejejejejg
dd Zejdejej
ejejejejgdd Zejdejejejejejejejejejg	dd Zejdejejejejejejejejejg	dd	 Zejd
ejdfejdfejdfejdfejdfejdfejejfejejfejejfg	dd Zejdejejejejejejejejejg	dd Z dS )TestInvariantUfuncsr   c                 C   sJ   dt j }||}t|t js"J |j|jks2J |j||jksFJ d S )N@)rs   r{   r/   r   r   r   r>   r   Zq_iq_or   r   r    test_invariant_scalare  s
    
z)TestInvariantUfuncs.test_invariant_scalarc                 C   s`   t g dtj tj }||}t|tjs2J |j|jksBJ t |j	||j	ks\J d S Ngffffff
g @gffffff$@)
r0   rv   rs   kgr   r/   r   r   r   r   r   r   r   r    test_invariant_arrayq  s
    z(TestInvariantUfuncs.test_invariant_arrayc                 C   s^   dt j }dt j }|||}t|t js.J |j|jks>J t|j||j||j d S )Nr   "@)	rs   r{   r|   r/   r   r   r   r   r   r>   r   q_i1q_i2r   r   r   r    test_invariant_twoarg_scalar|  s    


z0TestInvariantUfuncs.test_invariant_twoarg_scalarc                 C   s~   t g dtj tj }t g dtj tj }|||}t|tjsNJ |j	|j	ks^J t
|j||j||j	 d S )Nr   rb   g      g    .A)r0   rv   rs   r   r   gusr/   r   r   r   r   r   r   r   r   r    test_invariant_twoarg_array  s    
z/TestInvariantUfuncs.test_invariant_twoarg_array)r   	arbitraryr[   c                 C   s\   t g dtj tj }|||}t|tjs4J |j|jksDJ t|j	||j	| d S r   )
r0   rv   rs   r   r   r/   r   r   r   r   )r>   r   r  r   r   r   r   r    #test_invariant_twoarg_one_arbitrary  s
    
z7TestInvariantUfuncs.test_invariant_twoarg_one_arbitraryc                 C   sb   dt j }dt j }tt j}||| W d    n1 s@0    Y  d|jjd v s^J d S Nr   r   ri   r   rs   r{   r   r"   r#   r   r   r$   r>   r   r   r  r   r   r   r    #test_invariant_twoarg_invalid_units  s
    

(z7TestInvariantUfuncs.test_invariant_twoarg_invalid_unitsN)!rU   rV   rW   r"   rp   rq   r0   absoluteZfabsZconj	conjugatenegativeZspacingZrintZfloorZceilZpositiver   r   rC   rD   ZhypotZmaximumZminimumZ	nextafterr   modZfmodr  r  infr  r  r   r   r   r    r   c  s@   


	
	
r   c                	   @   s   e Zd Zejdejejej	ej
ejejgdd Zejdejejej	ej
ejejgdd Zejdejejejejfdd Zejdd	d
 ZdS )TestComparisonUfuncsr   c              	   C   sZ  t g dtj tj }t g dtj tj }|||}t|tjrNJ |j	t
ks\J t |||j||jks~J ||| d}t|tjrJ |j	t
ksJ t |||| tjdksJ t n tjdtd dt jt jfD ]&}||| |||t t|  q||t dt jt jg W d    n1 sL0    Y  d S )Nr   r  r]   ignore)categoryr[   )r0   rv   rs   r   r   r  Msr/   r   r   boolr   r   r   r   ru   r(   r)   r*   RuntimeWarningr  nanoneslen)r>   r   r   r  r   Zq_o2Zarbitrary_unit_valuer   r   r    test_comparison_valid_units  s(    
"

z0TestComparisonUfuncs.test_comparison_valid_unitsc                 C   sb   dt j }dt j }tt j}||| W d    n1 s@0    Y  d|jjd v s^J d S r	  r
  r  r   r   r    test_comparison_invalid_units  s
    

(z2TestComparisonUfuncs.test_comparison_invalid_unitsc                 C   s`   dt jt j t jddgtj }||}t|tjr6J |jtksDJ t 	|||j
ks\J d S Nr^   ra   r[   )r0   r  r  rs   r{   r/   r   r   r  r   r   )r>   r   r   outr   r   r    test_onearg_test_ufuncs  s
    z,TestComparisonUfuncs.test_onearg_test_ufuncs*ignore:.*invalid value encountered in signc                 C   s|   dt jt j t jddgtj }t |}t|tjr8J |j|jksHJ t 	|t |j
kt |t |j
@ B sxJ d S r  )r0   r  r  rs   r{   signr/   r   r   r   r   isnan)r>   r   r  r   r   r    	test_sign  s    
zTestComparisonUfuncs.test_signN)rU   rV   rW   r"   rp   rq   r0   greaterZgreater_equalZlessZ
less_equalZ	not_equalequalr  r  isfiniteZisinfr"  signbitr  r*   r#  r   r   r   r    r    s    



r  c                   @   s4  e Zd Zejddedgdd Zejddedgdd Z	ejddedgdd	 Z
ejddedgd
d Zejddedgdd Zdd Zejddedgdd Zdd Zdd Zejdejejfdd Zejdejejfdd Zejddd ZdS )TestInplaceUfuncsr   r^   rb   c                 C   s   |t j }|}tj||d ||u s(J |jt jks8J |t j t j}|}tj||d ||u shJ |jt jksxJ t|j	|j	 d S )Nr  )
rs   Zradr0   rr   r   ru   r}   degr   r   )r>   r   r   checks2check2r   r   r    test_one_argument_ufunc_inplace  s    
z1TestInplaceUfuncs.test_one_argument_ufunc_inplacec                 C   s.  |t j }|}tj||d ||u s(J t|jt|ksBJ |jt ju sRJ tj||d ||u slJ t|jt|ksJ |jt ju sJ tj	||d ||u sJ t|jt	|ksJ |jt ju sJ tj
|d |d ||u sJ t|jt
|d ksJ |jt ju s*J dS )z?Check inplace works with non-quantity input and quantity outputr)  rb   N)rs   r{   r0   r  r   r   r   ru   rF   r   ry   rx   r>   r   r   r+  r   r   r    !test_one_argument_ufunc_inplace_2  s$    
 z3TestInplaceUfuncs.test_one_argument_ufunc_inplace_2c                 C   s   d| t j t j }| }| }|}t||| ||u sBJ |jt jksRJ |t j}|}t||| ||u s|J |jt jksJ |t j}|}	t||| |	|u sJ |	jt jksJ | }
|
}t|
|
| ||
u sJ |jt jksJ d S )Nr   )	rs   r   r{   copyr0   r   r   ru   r}   )r>   r   r   Zv_copytmpr+  v2r-  Zv3check3Zv4Zcheck4r   r   r    *test_one_argument_two_output_ufunc_inplace  s,    z<TestInplaceUfuncs.test_one_argument_two_output_ufunc_inplacec                 C   s   |t j }|}|d }||u s"J t|j|d ks:J |t j }||u sPJ |jt jt j ksfJ |dt j 9 }||u sJ t||t j ksJ d S )Nr]   )rs   cycler0   r   r   r   r   r/  r   r   r    !test_two_argument_ufunc_inplace_1(  s    

z3TestInplaceUfuncs.test_two_argument_ufunc_inplace_1c                 C   s4  |t j }|}tj|||d ||u s*J |jt jks:J tt j |dt j	 7 }W d    n1 sj0    Y  ||u sJ |jt jksJ tjdt j
 ||d ||u sJ |jt jksJ tjdt j
 ||d ||u sJ |jt j
ksJ tjdt j ||d ||u sJ |jt j
t j ks0J d S )Nr)  r^   r]   )rs   r6  r0   rz   r   rx   r"   r#   r   r{   r*  rC   r   r   r/  r   r   r    !test_two_argument_ufunc_inplace_26  s$    
,z3TestInplaceUfuncs.test_two_argument_ufunc_inplace_2c                 C   s   t g dtj }t jt g dt g dd |d t |jt g dksZJ |jtju sjJ t jt g dt g dd |d t	|jt dd |jtj
u sJ d S )Nr   r]   r)  )r_   r   r   r^   )r0   rv   rs   ru   rC   r   r   r   rz   r   rx   r>   r   r   r   r    !test_two_argument_ufunc_inplace_3K  s    ((z3TestInplaceUfuncs.test_two_argument_ufunc_inplace_3c                 C   s8  |t j }dt j }| }| }tj||||fd}|d |u rP|d |u sTJ |jt jksdJ |j|jkstJ | }tj||||fd}|d |u r|d |u sJ |jt jksJ |j|jksJ | }	| }
tj|	||	|
fd}|d |	u r|d |
u sJ |	jt jks"J |
j|jks4J d S )Nrc   r)  r   rf   )rs   r{   r   r1  r0   r   r   ru   )r>   r   r   r   Zv1r2  r+  r3  r-  Zv3aZv3br4  r   r   r    *test_two_argument_two_output_ufunc_inplaceT  s&    

 z<TestInplaceUfuncs.test_two_argument_two_output_ufunc_inplacec                 C   s   t dtj }| }|d d d }|dtj 7 }t |d d d |d d d ksZJ t |dd d |dd d ksJ d S )Nrb   rg   r^   rf   )r0   r   rs   r{   r1  r   r   )r>   r   Zs_copyr,  r   r   r    &test_ufunc_inplace_non_contiguous_dataj  s    &z8TestInplaceUfuncs.test_ufunc_inplace_non_contiguous_datac                 C   s<  t jg dt jtjd}|td9 }|jt ju s6J |jtjksFJ t jg dt jtjd}|dt j 7 }|jt ju s|J |jtjksJ t jg dt jtjd}|t jdt jtj	d7 }|jt ju sJ |jtjksJ t jg dt jtjd}t
t( |t jdt jtj	d7 }W d   n1 s.0    Y  dS )zCheck that inplace operations check properly for casting.

        First two tests that check that float32 is kept close #3976.
        )rf   rg   rh   r   r   
   g      4@N)rs   r   r{   r0   Zfloat32r   r   r|   Zint32Zint64r"   r#   r~   Zmm)r>   Za1Za2Za3Za4r   r   r    %test_ufunc_inplace_non_standard_dtypes  s    z7TestInplaceUfuncs.test_ufunc_inplace_non_standard_dtyper   c                 C   s   t g dtj tj }t g dtj tj }t |jt	}||j
||j|d t |jt	}||||d}||u sJ t|t ju sJ |jt	ksJ t ||ksJ d S )Nr   r  r)  )r0   rv   rs   r   r   r  r  emptyshaper  r   r   r   r   ndarrayr   r   )r>   r   r   r  r+  r   r   r   r   r    test_comparison_ufuncs_inplace  s    z0TestInplaceUfuncs.test_comparison_ufuncs_inplacec                 C   s   dt jt j t jddgtj }t |jt}||j|d t |jt}|||d}||u s`J t	|t j
u srJ |jtksJ t |||jksJ d S Nr^   ra   r[   r)  )r0   r  r  rs   r{   r?  r@  r  r   r   rA  r   r   )r>   r   r   r+  r   r  r   r   r    test_onearg_test_ufuncs_inplace  s    z1TestInplaceUfuncs.test_onearg_test_ufuncs_inplacer   c                 C   s   dt jt j t jddgtj }t |j|j}t j|j	|d t |j|j}t j||d}||u shJ t
|t ju szJ |j|jksJ t |t |j	kt |t |j	@ B sJ d S rC  )r0   r  r  rs   r{   r?  r@  r   r!  r   r   rA  r   r"  )r>   r   r+  r   r  r   r   r    test_sign_inplace  s    z#TestInplaceUfuncs.test_sign_inplaceN)rU   rV   rW   r"   rp   rq   r0   r   r.  r0  r5  r7  r8  r:  r;  r<  r>  r%  r$  rB  r&  r'  rD  r*   rE  r   r   r   r    r(    s*   




	
	


r(  clipzno clip ufunc availabler   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )TestClipzTest the clip ufunc.

    In numpy, this is hidden behind a function that does not backwards
    compatibility checks.  We explicitly test the ufunc here.
    c                 C   s   t jjj| _d S r'   )r0   r5   r6   rF  rA   r   r   r    setup  s    zTestClip.setupc                 C   s   t ddtj }dtj }dtj }| |||}|j|jksDJ | |j|	|j|	|j|j }t 
||ks|J d S )Nra   rb   }   I+v?)r0   r   rs   r{   r   r|   rF  r   r   r   r   )r>   r   q_minq_maxr   r   r   r   r    test_clip_simple  s    


zTestClip.test_clip_simplec                 C   s<  t ddtj }dtj }| |t j |}| |jt j ||j	|j	 }t 
||ksbJ | ||t j}| |j||j	t j|j	 }t 
||ksJ | |t |j|}| |jd||j	|j	 }t 
||ksJ | |t |jt |jt j}	| |jdt j|j	 }
t 
|	|
ks8J d S )Nra   rb   rJ  r   )r0   r   rs   r{   r|   rF  r  r   r   r   r   Zzerosr@  Zfull)r>   r   ZqlimZresult1Z	expected1Zresult2Z	expected2Zresult3Z	expected3Zresult4Z	expected4r   r   r    test_clip_unitless_parts  s    
" "z!TestClip.test_clip_unitless_partsc                 C   sZ   t ddtj }| |dtj d}| |dd}|jtjksDJ t ||ksVJ d S Nra   rb      r   r]   )r0   r   rs   ru   rF  r   r   r   )r>   r   r   r   r   r   r    test_clip_dimensionless  s
    z TestClip.test_clip_dimensionlessc                 C   s`   t dd}| |dtj dtj }t|tjs6J | |ddtj }t ||ks\J d S rO  )	r0   r   rF  rs   r   ru   r/   r   r   )r>   ar   r   r   r   r    test_clip_ndarray  s
    zTestClip.test_clip_ndarrayc                 C   s   t ddtj }dtj }dtj }| |j||j	||j	|j	 }| j||||d}||u sjJ t 
||ks|J d S )Nra   rb   rI  rJ  r)  )r0   r   rs   r{   r   r|   rF  r   r   r   r   )r>   r   rK  rL  r   r   r   r   r    test_clip_quantity_inplace  s    


z#TestClip.test_clip_quantity_inplacec                 C   s   t dd}t |tj }| |ddtj }| j|dtj dtj |d}||u sZJ |jtjksjJ t 	||ks|J d S )Nra   rb   r]   r   rP  r)  )
r0   r   Z
zeros_likers   r{   rF  ru   r   r   r   )r>   rR  r   r   r   r   r   r    &test_clip_ndarray_dimensionless_output  s    z/TestClip.test_clip_ndarray_dimensionless_outputc                 C   s  t ddtj }ttj$ | |ddtj  W d    n1 sH0    Y  ttj& | |j	ddtj  W d    n1 s0    Y  ttj | |dd W d    n1 s0    Y  ttj | |dd W d    n1 s0    Y  d S )Nra   rb   r   rf   r[   r^   )
r0   r   rs   r{   r"   r#   r   rF  r   r   r   r   r   r    test_clip_errors  s    24,zTestClip.test_clip_errorsN)rU   rV   rW   ro   rH  rM  rN  rQ  rS  rT  rU  rW  r   r   r   r    rG    s   


rG  c                   @   s    e Zd ZdZdd Zdd ZdS )TestUfuncAtzTest that 'at' method for ufuncs (calculates in-place at given indices)

    For Quantities, since calculations are in-place, it makes sense only
    if the result is still a quantity, and if the unit does not have to change
    c           	   	   C   s  t dtj }t ddg}|j }t j|| t j|| t 	|j|ksXJ |j
tju shJ tt t j|| W d    n1 s0    Y  ttj t j|| W d    n1 s0    Y  t dtj }|j }t j|| t j|| t 	|j|ks&J |j
tju s8J t dtj }|j }t j|| t j|| t 	|j|ksJ |j
tju sJ t dtj }ttj t j|| W d    n1 s0    Y  |j }tt  t j|| t j|| t|j| t dtj }ttj t j|| W d    n1 sf0    Y  W d    n1 s0    Y  d S )Nrb   rf   rg   )r0   r   rs   r{   rv   r   r1  r  atr   r   r"   r#   r~   r&  r   r   ru   r   rx   rr   Zadd_enabled_equivalenciesZdimensionless_anglesr   rt   )	r>   r   rN   ZqvdZdvrR  avZadr   r   r    test_one_argument_ufunc_at  sD    
,,

.
z&TestUfuncAt.test_one_argument_ufunc_atc                 C   s  t dtj }t ddg}|j }t j||dtj	  t j||d t 
|j|ksbJ |jtju srJ ttj& t j||dtj  W d    n1 s0    Y  ttj& t j||dtj  W d    n1 s0    Y  t dtj }|j }t j||dtj  t j||d t 
|j|ksLJ t dtj }t j||d t 
|j|ksJ tt& t j||dtj	  W d    n1 s0    Y  d S )Nrb   rf   rg   r^   g     @@r]   )r0   r   rs   r{   rv   r   r1  rC   rY  r|   r   r   r"   r#   r   r   r   ru   r~   r$  )r>   r   rN   r+  r   r   r    test_two_argument_ufunc_atD  s*    
44
z&TestUfuncAt.test_two_argument_ufunc_atN)rU   rV   rW   ro   r\  r]  r   r   r   r    rX    s   2rX  c                   @   s    e Zd ZdZdd Zdd ZdS )!TestUfuncReduceReduceatAccumulatezTest 'reduce', 'reduceat' and 'accumulate' methods for ufuncs

    For Quantities, it makes sense only if the unit does not have to change
    c                 C   s   t dtj }t g d}tt t j	| W d    n1 sJ0    Y  tt t j
| W d    n1 s0    Y  tt t j|| W d    n1 s0    Y  d S Nrb   )r      rf      )r0   r   rs   rx   rv   r"   r#   r   rr   reduce
accumulatereduceat)r>   r   rN   r   r   r    )test_one_argument_ufunc_reduce_accumulatei  s    **zKTestUfuncReduceReduceatAccumulate.test_one_argument_ufunc_reduce_accumulatec                 C   s  t dtj }t g d}|j }t j|}t j|}|j|ksNJ |j	tju s^J t j
|}t j
|}t |j|ksJ |j	tju sJ t j||}t j||}	t |j|	ksJ |j	tju sJ tt t j| W d    n1 s0    Y  tt t j
| W d    n1 s@0    Y  tt t j|| W d    n1 sz0    Y  ttj t j| W d    n1 s0    Y  ttj t j
| W d    n1 s0    Y  ttj t j|| W d    n1 s*0    Y  t dtj }|j }t j|}
t j|}|
j|ksvJ |
j	tju sJ t j
|}t j
|}t |j|ksJ |j	tju sJ t j||}t j||}t |j|ksJ |j	tju sJ d S r_  )r0   r   rs   r{   rv   r   r1  rC   rb  r   rc  r   rd  r"   r#   r~   r$  r   r   ru   )r>   r   rN   r+  Zs_add_reduceZcheck_add_reduceZs_add_accumulateZcheck_add_accumulateZs_add_reduceatZcheck_add_reduceatZs_multiply_reduceZcheck_multiply_reduceZs_multiply_accumulateZcheck_multiply_accumulateZs_multiply_reduceatZcheck_multiply_reduceatr   r   r    )test_two_argument_ufunc_reduce_accumulatet  sR    
,,.,,.
zKTestUfuncReduceReduceatAccumulate.test_two_argument_ufunc_reduce_accumulateN)rU   rV   rW   ro   re  rf  r   r   r   r    r^  c  s   r^  c                   @   s    e Zd ZdZdd Zdd ZdS )TestUfuncOuterz{Test 'outer' methods for ufuncs

    Just a few spot checks, since it uses the same code as the regular
    ufunc call
    c                 C   sJ   t dtj }tt t j| W d    n1 s<0    Y  d S )Nrb   )	r0   r   rs   rx   r"   r#   r   rr   outerr9  r   r   r    test_one_argument_ufunc_outer  s    z,TestUfuncOuter.test_one_argument_ufunc_outerc                 C   sL  t dtj }t dtj }|j}|j}t j||}t j||}t |j|ks\J |j	|j	|j	 ksrJ t
tj t j|| W d    n1 s0    Y  t d|j	 }|j}t j||}	t j||}
t |	j|
ksJ |	j	|j	u sJ t j||}t j||}t|t ju s4J t ||ksHJ d S )Nrb   r]   )r0   r   rs   r{   r   r   r   rh  r   r   r"   r#   r   rC   r$  r   rA  )r>   s1r,  Zcheck1r-  Zs12_multiply_outerZcheck12_multiply_outerZs3r4  Zs13_add_outerZcheck13_add_outerZs13_greater_outerZcheck13_greater_outerr   r   r    test_two_argument_ufunc_outer  s(    ,z,TestUfuncOuter.test_two_argument_ufunc_outerN)rU   rV   rW   ro   ri  rk  r   r   r   r    rg    s   rg  )specialc                   C   sd   t jtjvsJ t dtj  t jtjv s0J tt jtj	rPt jtjv s`J nt jtjvs`J dS )z,Check that scipy gets loaded upon first use.r^   N)
spserfr9   r;   rs   r   r/   erfinvr0   r   r   r   r   r    test_scipy_registration  s    rp  c                   @   s  e Zd Zejdedd Zejdedd Zejdedd Z	ejde
jfdd	 Zejde
jfd
d Zejde
jfdd Ze
je
je
je
je
je
je
je
je
je
je
je
je
je
je
jfZejdedd Z ejdedd Z!ejdedd Z"dS )TestScipySpecialUfuncsr   c                 C   s   t d | d S r'   )r   r   r   r   r   r    test_erf_scalar  s    z&TestScipySpecialUfuncs.test_erf_scalarc                 C   s   t d | d S r'   )r   r   r   r   r   r    test_erf_array  s    z%TestScipySpecialUfuncs.test_erf_arrayc                 C   s   t d | d S r'   )r   r   r   r   r   r    test_erf_invalid_units  s    z-TestScipySpecialUfuncs.test_erf_invalid_unitsc                 C   s   t d | d S r'   )r   r   r   r   r   r    r     s    z'TestScipySpecialUfuncs.test_cbrt_scalarc                 C   s   t d | d S r'   )r   r   r   r   r   r    r     s    z&TestScipySpecialUfuncs.test_cbrt_arrayc                 C   sH  |dt j dt j dt j }t|jtj |jt j	ks<J |dt j dt j dt j }t|jdt j 
t j	j |jt j	ksJ |dt j dt j dt j }t|jdt j 
t j	j |dt j	 dt j dt j }t|jd |jt j	ksJ tt. |dt j dt j dt j  W d    n1 s:0    Y  d S )Nr`   r[   rY   r_   r]   r^   )rs   rt   ZarcminZarcsecr   r   r0   rw   r   rx   r}   r"   r#   r~   r{   r   r   )r>   r   r   r   Zq3Zq4r   r   r    test_radian  s    z"TestScipySpecialUfuncs.test_radianc                 C   sR   |dt j dt j  dt j dt j  }|jt jks:J |j|ddksNJ d S )Nr]   r_   r   r^   rZ   r   r   r   r   r    test_jv_scalar  s    *z%TestScipySpecialUfuncs.test_jv_scalarc              	   C   s   |t dtj dtj  t g dtj dtj  }|jtjksJJ t |j|t dt g dkstJ |t dtj dtj  t g dtj dtj	  }|jtjksJ t
|j|t dt g d d S )Nrh   r^   r   r   r   r   )r0   r  rs   r{   rv   r   ru   r   r   r   r   r   r   r   r    test_jv_array"  s"    

z$TestScipySpecialUfuncs.test_jv_arrayc                 C   sf   t t,}|dtj dtj tj  W d    n1 s<0    Y  |jjd d	|j
ksbJ d S r   )r"   r#   r~   rs   r   r{   r   r   r$   r   rU   r   r   r   r    test_jv_invalid_units3  s
    :z,TestScipySpecialUfuncs.test_jv_invalid_unitsN)#rU   rV   rW   r"   rp   rq   erf_like_ufuncsrr  rs  rt  rm  r   r   r   rx   ru  ZjvZjnZjveZynZyvZyveZknZkvZkveZivZiveZhankel1Zhankel1eZhankel2Zhankel2eZjv_like_ufuncsrv  rw  rx  r   r   r   r    rq    s,   





 

rq  )KZconcurrent.futuresrP   r(   collectionsr   r"   Znumpyr0   Znumpy.testingr   Zerfar   r=   Zastropyr   rs   Zastropy.unitsr   r9   Z(astropy.units.quantity_helper.convertersr   Z%astropy.units.quantity_helper.helpersr	   Z"astropy.utils.compat.optional_depsr
   r   r   r   rp   skipr!   r&   r,   r-   rX   r   r   r  r(  r   hasattrr5   r6   rG  rX  r^  rg  Zscipyrl  rm  rn  ZerfcZerfcxZerfiZgammaZgammalnZloggammaZgammasgnZpsiZrgammaZdigammaZwofzZdawsnZentrZexprelr   r   r   Zexp10ry  r/   ro  Zerfcinvrp  rq  r   r   r   r    <module>   s`   



9 Q  )H: QTXJ)
