a
    ߙfb                     @   sj  d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZ ejejejgZejejejgZejejejgZejejejejd  ejfZ G dd dZ!d	d
 Z"dd Z#dd Z$dd Z%G dd dZ&G dd dZ'G dd dZ(dd Z)dd Z*G dd dZ+dd Z,dd  Z-G d!d" d"Z.G d#d$ d$Z/G d%d& d&Z0G d'd( d(Z1G d)d* d*Z2dS )+z/
    Test the Logarithmic Units and Quantities
    N)assert_allclose)assert_quantity_allclose)units	constants   c                   @   s   e Zd Zdd Zejdeee	dd Z
ejdedd Zejd	eeed
d Zdd Zejdee	ejg edd Zdd Zdd Zdd ZdS )TestLogUnitCreationc                 C   sF   t jt jdksJ t jt jdks,J t jt jdksBJ dS )z-Check logarithmic units are set up correctly.g?g      N)udBtodexmagself r   Clib/python3.9/site-packages/astropy/units/tests/test_logarithmic.pytest_logarithmic_units   s    z*TestLogUnitCreation.test_logarithmic_unitszlu_unit, lu_clsc                 C   s.   t |tjsJ t|sJ |j|u s*J d S N)
isinstancer	   ZUnitBasecallableZ_function_unit_class)r   lu_unitlu_clsr   r   r   test_callable_units"   s    z'TestLogUnitCreation.test_callable_unitsr   c                 C   s&   | }||j ksJ |j |ks"J d S r   )_default_function_unit)r   r   lur   r   r   .test_equality_to_normal_unit_for_dimensionless(   s    zBTestLogUnitCreation.test_equality_to_normal_unit_for_dimensionlesslu_unit, physical_unitc                 C   s*   ||}|j |ksJ |j|jks&J dS )zuCreate a LogUnit subclass using the callable unit and physical unit,
        and do basic check that output is right.N)physical_unitfunction_unitr   )r   r   r   lu1r   r   r   test_call_units.   s    z#TestLogUnitCreation.test_call_unitsc                 C   sp   t t tg  W d    n1 s*0    Y  t t tt  W d    n1 sb0    Y  d S r   )pytestraises	TypeErrorr	   r   
ValueErrorr   r   r   r   test_call_invalid_unit7   s    (z*TestLogUnitCreation.test_call_invalid_unitzlu_cls, physical_unitc                 C   s   ||}|j |ksJ |j|jks&J ||d|j d}|j |ksFJ |jtd|j ks`J tt ||tj W d   n1 s0    Y  dS )zjCreate a LogUnit subclass object for given physical unit,
        and do basic check that output is right.r   r   N)	r   r   r   r	   Unitr!   r"   r$   m)r   r   r   r   lu2r   r   r   test_subclass_creation=   s    z*TestLogUnitCreation.test_subclass_creationc                 C   s   dt j> }t|t jsJ |jt jks*J |jdks8J td}|t j> }t|t js\J |jt jkslJ t|j|ksJ d|d< t|j|ksJ dt j	> }t|t jsJ |jt j	ksJ |jdksJ d S )N      ?      $@        	   )
r	   ABmagr   	MagnitudeunitvaluenparangeallSTmag)r   r   Za2q2r   r   r   test_lshift_magnitudeN   s    



z)TestLogUnitCreation.test_lshift_magnitudec                 C   s  t t jt j t jd  t j }tdt t j }|j	}||> }t
|||ks^J |}||K }||u svJ t
|j|jksJ |j|ksJ t t dt j & |t j}|t jK }W d    n1 s0    Y  ||u sJ t|j	| |jt jksJ d S )Nr   r,   i|  )r	   r   ergscmHzr3   r4   Jyphysicalr5   r   r2   r1   Zadd_enabled_equivalenciesspectral_densityAASTr6   r   )r   Zmag_fnu_cgsr(   ZjyZm2str   r   r   test_ilshift_magnitudea   s     "(z*TestLogUnitCreation.test_ilshift_magnitudec                 C   s   t dttj }ttj |tj>  W d    n1 sB0    Y  ttj |tj>  W d    n1 sx0    Y  ttj |tjK }W d    n1 s0    Y  ttj |tjK }W d    n1 s0    Y  d S Nr,   )	r3   r4   r	   r   r=   r!   r"   
UnitsErrorr6   )r   r(   r   r   r   test_lshift_errorsv   s    (((z&TestLogUnitCreation.test_lshift_errorsN)__name__
__module____qualname__r   r!   markparametrizeziplu_unitslu_subclassesr   r   	itertoolsproduct	pu_sampler    r%   r	   LogUnitr*   r8   rC   rF   r   r   r   r   r      s"   




r   c                   C   s   t dtj jdtj tjd  tj tj  t dtj jdtj tjd  tj tj	  t dtj
 jtj t dtj jtjdtj dtj d    d S )Ng5r+   r   gLHr         @r,   )r   r	   r6   r>   r9   r;   r:   r@   r/   r<   M_bolcZL_bol0m_bolr3   pipcr   r   r   r   test_predefined_magnitudes   s    rY   c                   C   s|   t dt jksJ t dt jks(J t dt jks<J t dt jksPJ t dt jksdJ t dt jksxJ d S )NZSTfluxZABfluxZBolZbolrA   ZAB)r	   r   r6   r/   rT   rV   r   r   r   r    test_predefined_reinitialisation   s    rZ   c                   C   sl   t t j t jksJ t t j t jks4J t t j t jksNJ t t j t jkshJ dS )z Ensure round-tripping; see #5015N)r	   r'   r6   	to_stringr/   rT   rV   r   r   r   r    test_predefined_string_roundtrip   s    r\   c                  C   sX   t t j} t t j}t t jd }||  }| |ks<J | |ksHJ | |ksTJ dS )z*Check __ne__ works (regression for #5342).r   N)r	   r   r=   r   )r   r)   lu3lu4r   r   r   test_inequality   s    r_   c                   @   s   e Zd Zdd ZdS )TestLogUnitStringsc                 C   s|  t t j}t|dksJ t|dks,J |ddks>J tt |d W d   n1 sh0    Y  tt |jdd W d   n1 s0    Y  t 	 }t|dksJ t|d	ksJ | d
ksJ t j
t jdt j d}t|dksJ t|dksJ | dks,J t t j}|ddksLJ |ddks`J | |dksxJ dS )z:Do some spot checks that str, repr, etc. work as expected.zmag(Jy)zUnit("mag(Jy)")ZgenericZfitsNZcds)formatr   zUnit("dex(1)")zdex(1)r   r&   z	2 mag(Jy)zMagUnit("Jy", unit="2 mag")zmag(ct)Zlatexz3$\mathrm{mag}$$\mathrm{\left( \mathrm{ct} \right)}$)r	   r   r=   strreprr[   r!   r"   r$   r   MagUnitZctZ_repr_latex_)r   r   r)   r]   r^   r   r   r   test_str   s(    (*zTestLogUnitStrings.test_strN)rG   rH   rI   re   r   r   r   r   r`      s   r`   c                   @   sx   e Zd Zejdeee	dd Z
ejdedd Zejdeeee	dd	 Zd
d Zdd Zdd ZdS )TestLogUnitConversionr   c                 C   sd  ||}| |sJ ||ddks*J | |s8J ||ddksLJ td| }| |shJ ||ddks|J | |sJ t||dddd tddd}t||||||dd tj}| |rJ t	tj
 || W d	   n1 s0    Y  | |r(J t	tj
 || W d	   n1 sV0    Y  d	S )
zfCheck various LogUnit subclasses are equivalent and convertible
        to their non-log counterparts.r-   r+   g       @g      ?V瞯<Zatolr,      N)is_equivalentr   r	   r'   r   r3   linspacegr!   r"   rE   )r   r   r   r   Zpur2   Zpu2r   r   r   test_physical_unit_conversion   s&    *z3TestLogUnitConversion.test_physical_unit_conversionr   c                 C   s   t ddd}|tj}||js(J t||j|| |tj}||jrVJ t	
tj ||j| W d   n1 s0    Y  dS )zCheck that conversion to logarithmic units (u.mag, u.dB, u.dex)
        is only possible when the physical unit is dimensionless.r-   r,   ri   N)r3   rk   r	   dimensionless_unscaledrj   r   r   r   r=   r!   r"   rE   )r   r   valuesr   r)   r   r   r   test_container_unit_conversion   s    

z4TestLogUnitConversion.test_container_unit_conversionz!flu_unit, tlu_unit, physical_unitc           
      C   s:  t ddd}||}||}||s,J t|||j|j t|||||j|j  |td| }||sJ t||||||dd ||tj	d }||sJ t||||||dd |tj
}	||	rJ ttj ||	| W d   n1 s,0    Y  dS )	zCheck various LogUnit subclasses are equivalent and convertible
        to each other if they correspond to equivalent physical units.r-   r,   ri         Y@rg   rh   r   N)r3   rk   rj   r   r   r   r	   r'   Z	to_systemsirl   r!   r"   rE   )
r   Zflu_unitZtlu_unitr   ro   ZfluZtluZtlu2Ztlu3Ztlu4r   r   r   test_subclass_conversion   s$    
z.TestLogUnitConversion.test_subclass_conversionc                 C   s   t t j}| t t j ks(J | jjt jt jgksDJ |jt t jjks\J |jjjt jt jgksvJ |j	t t jj	ksJ |j	jjt j
t jgksJ d S r   )r	   r   r=   	decomposer   baseskgr:   rr   cgsrl   r   r   r   r   r   test_unit_decomposition  s    z-TestLogUnitConversion.test_unit_decompositionc                 C   s   t t j}|tsJ d S r   )r	   r   r=   rj   rQ   rx   r   r   r   )test_unit_multiple_possible_equivalencies!  s    z?TestLogUnitConversion.test_unit_multiple_possible_equivalenciesc                 C   s^   t tj,}dtj dtj  tj W d   n1 s>0    Y  dt|jv sZJ dS )zCheck that "dimensionless" magnitude units include a message in their
        exception text suggesting a possible cause of the problem.
        
   r   Nz9Did you perhaps subtract magnitudes so the unit got lost?)	r!   r"   r	   ZUnitConversionErrorr/   r   ZnJyrb   r2   )r   excinfor   r   r   'test_magnitude_conversion_fails_message%  s    :z=TestLogUnitConversion.test_magnitude_conversion_fails_messageN)rG   rH   rI   r!   rJ   rK   rO   rP   rM   rQ   rm   rp   rs   ry   rz   r}   r   r   r   r   rf      s   



	rf   c                   @   s   e Zd Zdd Zejdddd Zejdedd	 Z	d
d Z
ejdeje eejeejedej eddej fdd Zdd Zdd ZdS )TestLogUnitArithmeticc           	   
   C   s  t t j}tt j |t j  W d   n1 s80    Y  tt j t j|  W d   n1 sn0    Y  tt j ||  W d   n1 s0    Y  t jt jt jt jfD ]8}tt j ||  W d   q1 s0    Y  qt t j}tt j ||  W d   n1 s20    Y  tt j ||  W d   n1 sh0    Y  || t jksJ |t j }t j| }||fD ]}t	|t
|rJ ||jt j ksJ t t  J tt j ||j W d   n1 s0    Y  W d   n1 s00    Y  q|t j }t t  P ||jsjJ t|t jtdd ||jtd W d   n1 s0    Y  || }t	|t
|rJ |t jksJ ||j }t	|t
|rJ |t jksJ tt |t   W d   n1 s>0    Y  tt td|  W d   n1 sv0    Y  tt |g   W d   n1 s0    Y  tt d|  W d   n1 s0    Y  dS )zCheck that multiplication/division with other units is only
        possible when the physical unit is dimensionless, and that this
        turns the unit into a normal one.N      @rq      )r	   r   r=   r!   r"   rE   r(   rn   r   r   typer   set_enabled_equivalencieslogarithmicr   r   r;   rj   r   r3   r4   r#   objectslice)	r   r   r1   r)   tftrtt2Zt3r   r   r   test_multiplication_division/  sX    ((&(((

P
$
*,(z2TestLogUnitArithmetic.test_multiplication_divisionpowerr         ?r   r   c              	   C   sT  t t j}|dkr(|| t jksvJ nN|dkrB|| |ksvJ n4tt j ||  W d   n1 sl0    Y  t t j}|| }|dkr|t jksJ n|dkr||ksJ nt|t|rJ ||j	| ksJ |d|  }||j	ksJ t 
t  : t|t jtd||jtd W d   n1 sF0    Y  dS )zCheck that raising LogUnits to some power is only possible when the
        physical unit is dimensionless, and that conversion is turned off when
        the resulting logarithmic unit (such as mag**2) is incompatible.r   r   Nr+   r   )r	   r   r=   rn   r!   r"   rE   r   r   r   r   r   r   r   r3   r4   r   )r   r   r   r)   r   r   r   r   r   test_raise_to_powerp  s*    &z)TestLogUnitArithmetic.test_raise_to_powerotherc                 C   s   t t j}tt j ||  W d    n1 s60    Y  tt j ||  W d    n1 sj0    Y  tt j ||  W d    n1 s0    Y  d S r   )r	   r   r=   r!   r"   rE   )r   r   r   r   r   r   /test_addition_subtraction_to_normal_units_fails  s    &&zETestLogUnitArithmetic.test_addition_subtraction_to_normal_units_failsc                 C   sx   t t j}tt |d  W d    n1 s40    Y  tt |g d  W d    n1 sj0    Y  d S )Nr+   )r+          @r   )r	   r   r=   r!   r"   r#   )r   r   r   r   r   ,test_addition_subtraction_to_non_units_fails  s
    &zBTestLogUnitArithmetic.test_addition_subtraction_to_non_units_failsr    r   c                 C   s   t t j}t|dt j}|| }||j| s6J || }||j| sRJ || }||j| snJ || }|||j sJ dS )z.Check physical units are changed appropriatelyr   N)r	   r   r=   getattrrn   rj   r   )r   r   r   Zother_puZlu_sfZlu_srZlu_dfZlu_drr   r   r   test_addition_subtraction  s    z/TestLogUnitArithmetic.test_addition_subtractionc                 C   sX   t dddtj dt j d   }t |}t j| }|t jt j	 t j
 sTJ dS )z?for fun, a more complicated example of addition and subtractionDMr+   rS   r,   r   N)r	   r'   r3   rW   rX   r   r6   rj   r9   r:   r@   )r   dm0Zlu_dmZlu_absSTr   r   r   %test_complicated_addition_subtraction  s    $

z;TestLogUnitArithmetic.test_complicated_addition_subtractionc                 C   sb   t t j}| }||ksJ |jt jd ks2J | |ks@J |
 }||usRJ ||ks^J d S )N)r	   r   r=   r   )r   r   Zneg_luZpos_lur   r   r   test_neg_pos  s    z"TestLogUnitArithmetic.test_neg_posN)rG   rH   rI   r   r!   rJ   rK   r   rQ   r   r   r	   r   r=   r(   r'   rd   r   r   r   r   r   r   r   r~   .  s   A
 
 
r~   c                  C   s:   t t jt jd  } t| }t|}| |ks6J d S Nr   )r	   r   r;   r:   pickledumpsloads)r   r:   r)   r   r   r   test_pickle  s    

r   c                  C   sj   t t j} t t j}t t j}t| t|ks8J t| t|ksLJ | ||h}t|dksfJ d S r   )r	   r
   ZmWr(   hashlen)r   r)   r]   Zlusetr   r   r   test_hashable  s    
r   c                   @   s  e Zd Zejdeeej	g e
ejg dd Zejdeeedd Zejdeje eejeejedej ed	d
ej eejdej eejdej fdd Zejddeej dfdeej dfdeejejd  ej  eejfdeejejd  ej  eejffdd Zejde eejeejed	d
ej eejdej eejdej fdd Zdd Zdd ZdS )TestLogQuantityCreationzlq, luc                 C   s*   |j |ksJ t| d|u s&J dS )z5Check logarithmic quantities are all set up correctlyr+   N)Z_unit_classr   _quantity_class)r   lqr   r   r   r   test_logarithmic_quantities  s    z3TestLogQuantityCreation.test_logarithmic_quantitieszlq_cls, physical_unitc                 C   s   t dd}||| }|jj|ks(J |jj|jjks<J t|jj| t	
t ||| W d   n1 st0    Y  dS )zgCreate LogQuantity subclass objects for some physical units,
        and basic check on transformationsr+   r,   N)r3   r4   r1   r   r   r   r   r>   r2   r!   r"   r$   )r   Zlq_clsr   r2   Zlog_qr   r   r   r*     s    z.TestLogQuantityCreation.test_subclass_creationr1   r   r   r   r          c                 C   sB   t d|}|jjt|d|ks$J |jjt|dt ju s>J d S )NGz?r   r   )r	   r0   r1   r   r   r   rn   )r   r1   qr   r   r   test_different_units  s
    z,TestLogQuantityCreation.test_different_unitszvalue, unitr+   Nc                 C   sJ   t ||}||ksJ |jjt jks*J |jjt|d|jjksFJ d S )Nr   )r	   r0   r1   r   r   r   r   )r   r2   r1   r   r   r   r   test_function_values  s    z,TestLogQuantityCreation.test_function_valuesc                 C   s   d| }t |tjsJ |jdks&J |j|ks4J d|j }|| }|j|ksTJ |jj|jksfJ ||jdkszJ |jtj ddksJ |d }||ksJ d S )Ng      @rq   r   g      g?)	r   r	   r0   r2   r1   r   Zto_value_function_viewr   )r   r1   Zq1Zpvr7   Zq3r   r   r   test_indirect_creation  s    
z.TestLogQuantityCreation.test_indirect_creationc                 C   s   ddgt j t jd  }tt |t j W d    n1 sD0    Y  ddgt j }|t j}t	|t jsxJ |j
jt jksJ t||ksJ d S )Nrq   g     @@r   r   r   )r	   r;   r:   r!   r"   r#   viewDexr   r   r1   r   rn   r3   r5   )r   r   r   r   r   r   test_from_view  s    *z&TestLogQuantityCreation.test_from_viewc                 C   sj   t t j}tt j t d| W d   n1 s:0    Y  t jd|dd}t||ju sfJ dS )z4Check that we can use Quantity if we have subok=Truer+   NT)Zsubok)	r	   r   r@   r!   r"   ZUnitTypeErrorQuantityr   r   )r   r   r   r   r   r   test_using_quantity_class&  s
    *z1TestLogQuantityCreation.test_using_quantity_class)rG   rH   rI   r!   rJ   rK   rL   lq_subclassesr	   ZLogQuantityrN   rR   r   rO   rP   rQ   r*   r   r=   r(   r'   rd   r   r   Wr<   r   r   r   r   r   r   r   r   r     s<   



 $
**
,$
r   c                  C   s   g dt j } | j}|t j}t|t js0J |j| jks@J t|j| j g dt j	 }| jt j
t |d}|t jt |}t|t jsJ |j| jksJ t|j| j dS )z/Ensures we can convert from regular quantities.)r,   g      (@g      ,@)g\@ǧ_@gLb@ZequivalenciesN)r	   r6   r>   r   r   r0   r1   r   r2   r@   r=   r?   )ZmstZflux_lambdaZmst_roundtripZwaveZflux_nuZmst_roundtrip2r   r   r   /test_conversion_to_and_from_physical_quantities0  s    r   c                  C   s   dt t j } |  | ks J |  jjjt jt jgks>J | j	| ksLJ | j	jjjt jt jgkshJ | j
| ksvJ | j
jjjt jt jgksJ d S rD   )r	   r   r=   rt   r1   r   ru   rv   r:   rr   rw   rl   )r   r   r   r   test_quantity_decompositionA  s    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestLogQuantityViewsc                 C   s2   t tddt j | _t tdd| _d S )Nr+   r,         @)r	   r0   r3   r4   r=   r   lq2r   r   r   r   setupL  s    zTestLogQuantityViews.setupc                 C   s<   | j j}t|tju sJ d|d< t| j j|ks8J d S )Ng      r   )r   r2   r   r3   Zndarrayr5   )r   Zlq_valuer   r   r   test_value_viewP  s    z$TestLogQuantityViews.test_value_viewc                 C   sX   | j j}t|tju sJ |j| j jju s.J d|j |d< t| j j	|j	ksTJ d S )Nr      )
r   r   r   r	   r   r1   r   r3   r5   r2   )r   Zlq_fvr   r   r   test_function_viewV  s
    z'TestLogQuantityViews.test_function_viewc                 C   s   t t | jtj W d    n1 s.0    Y  | jtj}|jtj	u sVJ t
|j| jjksnJ |tj}t|jtju sJ |jjtjksJ t
|| jksJ d S r   )r!   r"   r#   r   r   r	   r   r   r1   r   r3   r5   r2   r0   r   rd   r   rn   )r   r7   lq3r   r   r   test_quantity_view]  s    ,z'TestLogQuantityViews.test_quantity_viewN)rG   rH   rI   r   r   r   r   r   r   r   r   r   K  s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestLogQuantitySlicingc                 C   s6  t tddt j }|d t dt j ks4J dt j |d< |d t dt j ks^J tt j dt j |d< W d    n1 s0    Y  tt j dt j	 |d< W d    n1 s0    Y  tt j$ t dt j |d< W d    n1 s
0    Y  |d t dt j ks2J d S )Nr+         &@r.   r,   rq   r   )
r	   r0   r3   r4   r=   r!   r"   rE   r(   r   r   lq1r   r   r   test_item_get_and_setl  s    ,,4z,TestLogQuantitySlicing.test_item_get_and_setc                 C   s:  t tddt j }dt j |dd< t|dd t dt j ksPJ tt j" dt j	 |dd< W d    n1 s0    Y  tt j" dt j
 |dd< W d    n1 s0    Y  tt j( t dt j	 |dd< W d    n1 s0    Y  t|d t dt j ks6J d S )Nr+   r,   rq   r      )r	   r0   r3   r4   r=   r5   r!   r"   rE   r(   r   r   r   r   r   test_slice_get_and_sety  s    &008z-TestLogQuantitySlicing.test_slice_get_and_setN)rG   rH   rI   r   r   r   r   r   r   r   k  s   r   c                   @   s2  e Zd Zdd Zejdddd Zdd Zejd	e	d
d Z
ejd	dej de  edej edej dedej  eddej fdd Zejd	e	dd Zejd	dej de  edej edej dedej  eddej fdd Zdd ZdS )TestLogQuantityArithmeticc              
   C   s  t tddt j }tt j |dt j   W d   n1 sH0    Y  tt j dt j |  W d   n1 s0    Y  tt j ||  W d   n1 s0    Y  t jt j	t j
fD ]8}tt j ||  W d   q1 s0    Y  qt tdd}tt j ||  W d   n1 sH0    Y  tt j ||  W d   n1 s~0    Y  tt j ||  W d   n1 s0    Y  |t d }|jt jksJ t|j|jd ksJ |t j }t j| }||fD ]}t|t|r.J |j|jjt j ksHJ t t  L tt j ||jj W d   n1 s0    Y  W d   n1 s0    Y  q|dt j  }|j|jjsJ t||jj|jd  dS )zCheck that multiplication/division with other quantities is only
        possible when the physical unit is dimensionless, and that this turns
        the result into a normal quantity.r+   r   Nr   r   g      I@r   )r	   r0   r3   r4   r=   r!   r"   rE   r(   r   r   r1   rn   r5   r2   r   r   r   r   r   r   r   r;   rj   r   r   )r   r   r1   r   rr   r   r   r   r   r   r     s>    ,,&((((

Rz6TestLogQuantityArithmetic.test_multiplication_divisionr   r   c              	   C   s  t tddt j }|dkr8t|| dksJ nT|dkrXt|| |ksJ n4tt j ||  W d   n1 s0    Y  t td}|| }|dkr|j	t j
u sJ t|jdksJ n|dkrt||ksJ nt|t|rJ |j	|j	j| ksJ t t  J tt j |t j
 W d   n1 sZ0    Y  W d   n1 sz0    Y  dS )zCheck that raising LogQuantities to some power is only possible when
        the physical unit is dimensionless, and that conversion is turned off
        when the resulting logarithmic unit (say, mag**2) is incompatible.r+   rS   r   r   Nr,   )r	   r0   r3   r4   r=   r5   r!   r"   rE   r1   rn   r2   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r     s&    &z-TestLogQuantityArithmetic.test_raise_to_powerc                 C   sN   t tddt j }tt ||  W d    n1 s@0    Y  d S )Nr+   rS   r	   r0   r3   r4   r=   r!   r"   r#   r   r   r   r   r   test_error_on_lq_as_power  s    z3TestLogQuantityArithmetic.test_error_on_lq_as_powerr   c                 C   s   t tddt j }d| }tt j ||  W d    n1 sJ0    Y  tt j ||  W d    n1 s~0    Y  tt j ||  W d    n1 s0    Y  d S )Nr+   r,   r   )r	   r0   r3   r4   r=   r!   r"   rE   )r   r   r   r   r   r   r   r     s    &&zITestLogQuantityArithmetic.test_addition_subtraction_to_normal_units_failsr   gQ@g@g=
ףp=@gGz@r   gQ@r   c                 C   s   t tddt j }|jt|jdt jt 	 d}|| }t
|j|j|  || }t
|j|j|  || }t
|j|j|  || }t
|j||j  dS )zCheck that addition/subtraction with quantities with magnitude or
        MagUnit units works, and that it changes the physical units
        appropriately.r+   r,   r   r   N)r	   r0   r3   r4   r=   r   r   r1   rn   r   r   r>   )r   r   r   other_physicallq_sfZlq_srlq_dfZlq_drr   r   r   r     s    z3TestLogQuantityArithmetic.test_addition_subtractionc                 C   s   t t j}t tdd|}tt j ||7 }W d    n1 sJ0    Y  t	|j
tddkspJ |j|ks~J tt j ||8 }W d    n1 s0    Y  t	|j
tddksJ |j|ksJ d S )Nr+   r,   )r	   r   r=   r0   r3   r4   r!   r"   rE   r5   r2   r1   )r   r   r   r   r   r   r   -test_inplace_addition_subtraction_unit_checks  s    &&zGTestLogQuantityArithmetic.test_inplace_addition_subtraction_unit_checksc                 C   s~   t tddt j }|jt|jdt jt 	 d}|
 }||7 }t|j|j|  |
 }||8 }t|j|j|  dS )zCheck that inplace addition/subtraction with quantities with
        magnitude or MagUnit units works, and that it changes the physical
        units appropriately.r+   r,   r   r   N)r	   r0   r3   r4   r=   r   r   r1   rn   r   copyr   r>   )r   r   r   r   r   r   r   r   r   !test_inplace_addition_subtraction  s    z;TestLogQuantityArithmetic.test_inplace_addition_subtractionc                 C   s   t dddtj dt j d   }t |}dt j }d| }|| }|jt j	t j
 t j sfJ t|j|jd tj dt j d   d dk sJ d	S )
z@For fun, a more complicated example of addition and subtraction.r   r+   rS   r,   r   r   rq   rg   N)r	   r'   r3   rW   rX   r   r6   r1   rj   r9   r:   r@   absr>   )r   r   ZDMmagZm_stZdmZM_str   r   r   r   (  s    $

z?TestLogQuantityArithmetic.test_complicated_addition_subtractionN)rG   rH   rI   r   r!   rJ   rK   r   r   rQ   r   r	   r   r0   r=   r(   r'   r   r   r   r   r   r   r   r   r     s.   2

"

"
r   c                   @   s   e Zd Zdd Zdd ZdS )TestLogQuantityComparisonsc                 C   sf   t tddt j }tt |dk W d    n1 s@0    Y  |dkrVJ |dksbJ d S )Nr+   r,   ar   r   r   r   r   'test_comparison_to_non_quantities_fails5  s
    &zBTestLogQuantityComparisons.test_comparison_to_non_quantities_failsc                 C   sP  t tddt j }t dt j }t||ktg dksHJ t||ktg dkshJ t dt j }t||ktg dksJ t||ktg dksJ t dt j }||krJ ||ksJ t	
t j ||k  W d    n1 s0    Y  dt j }t||ktg dksBJ t||k tg dksdJ t	
t j |dt j k W d    n1 s0    Y  t	
t j  ||jt j k W d    n1 s0    Y  t tdd}|jt j }t||ksJ t	
t j |dt j k  W d    n1 sB0    Y  d S )Nr+   rS   r   )TFF)FTFg      ?)r	   r0   r3   r4   r=   r5   Zarrayr   r(   r!   r"   rE   r2   r   )r   r   r   r   Zlq4Zq5Zlq6Zfv6r   r   r   test_comparison=  s0        (
"".0z*TestLogQuantityComparisons.test_comparisonN)rG   rH   rI   r   r   r   r   r   r   r   4  s   r   c                   @   sf   e Zd Zdd Zejdddd Zdd Zejdd	d
d Z	dd Z
ejdddd ZdS )TestLogQuantityMethodsc                 C   sT   t ddddttj | _t dddddt  | _| j| jf| _d S )Nr+   r   r   g      @r   r   )	r3   r4   Zreshaper	   r   r=   mJym1magsr   r   r   r   r   [  s    " zTestLogQuantityMethods.setupmethod)
ZmeanminmaxroundZtracestdvarptpdiffediff1dc                 C   s   | j D ]z}t|| }t|jt|j| jks6J |dv rR|jt ksJ q|dkrp|jtjd ksJ q|j|jksJ qd S )N)r   r   r   r   r   r   )	r   r   r3   r5   r2   r   r1   r	   r   )r   r   r   resr   r   r   test_always_ok`  s    

z%TestLogQuantityMethods.test_always_okc                 C   sB   | j D ]6}t|d|j d|j j|jddksJ qd S )Nr   rS   )r   r3   r5   Zclipr1   r2   )r   r   r   r   r   	test_clipn  s    
z TestLogQuantityMethods.test_clip)sumZcumsumZnansumc                 C   s~   t | j| }t|jt | jj| jks0J |j| jjksBJ tt	 t | j
|  W d    n1 sp0    Y  d S r   )r   r   r3   r5   r2   r   r1   r!   r"   r#   r   )r   r   r   r   r   r   test_only_ok_if_dimensionlesss  s    
z4TestLogQuantityMethods.test_only_ok_if_dimensionlessc                 C   s0   t | j| jj| jj| jjks,J d S r   )r3   r5   r   dotr2   r   r   r   r   test_dot|  s    zTestLogQuantityMethods.test_dot)ZprodZcumprodc                 C   st   t t t| j|  W d    n1 s.0    Y  t t t| j|  W d    n1 sf0    Y  d S r   )r!   r"   r#   r   r   r   )r   r   r   r   r   test_never_ok  s    ,z$TestLogQuantityMethods.test_never_okN)rG   rH   rI   r   r!   rJ   rK   r   r   r   r   r   r   r   r   r   r   Z  s   

r   )3__doc__r   rO   r!   Znumpyr3   Znumpy.testingr   Zastropy.tests.helperr   Zastropyr   r	   r   rU   r   r   ZdecibelrM   ZDexUnitrd   ZDecibelUnitrN   r   r0   ZDecibelr   rn   r(   rl   r:   r=   rQ   r   rY   rZ   r\   r_   r`   rf   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s>   kb 
T
  /&