
    HR-eM4                        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mZ d dlmZmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d d	lmZ ej                             e d
          d             Z!ej                             e d
           G d d                      Z"ej                             e d
          d             Z#ej                             e d
          d             Z$ej                             e d
          ej        %                    dg d          d                         Z&ej                             e d
          ej        %                    dg d          d                         Z'ej                             e d
          ej        %                    deeeeeeeeej(        ej)        ej*        ej+        ej,        ej-        ej.        ej/        g          d                         Z0dS )    N)StringIO)units)coreflrw)_z_at_scalar_value
z_at_value)WMAP1WMAP3WMAP5WMAP7WMAP9Planck13Planck15Planck18)allclose)	HAS_SCIPY)AstropyUserWarningztest requires scipy)reasonc                  .   t           } t          t          | j        dt          j        z            dd          sJ t          t          | j        dt          j        z            dd          sJ t          t          | j        dt          j        z            dd          sJ t          t          | j	        d	t          j
        z            d
d          sJ t          t          | j	        dt          j        z  d          dd          sJ t          t          | j        dt          j
        z  d          dd          sJ t          t          | j        dt          j
        z  d          dd          sJ t          j        t          j                  5  t          j        t$          d          5  t          | j        dt          j
        z  d           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          j        t          j                  5  t          j        t$          d          5  t          | j        dt          j
        z  d           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N   g&W	@ư>rtol   gCr?.   gʲb?g     @g=*?g9{	:@g|=)ztol   g&.>  )zmax,?      @)zminԭ T@fval is not bracketedmatch      ?      @)r   r   r   ageuGyrlookback_timedistmodmagluminosity_distanceMpcGpcangular_diameter_distancepytestraisesr   CosmologyErrorwarnsr   )cosmos    Hlib/python3.11/site-packages/astropy/cosmology/funcs/tests/test_funcs.pytest_z_at_value_scalarr9      s    EJuy!ae)44jtLLLLLLJu2AI>>RVWWWWWWJu}b15j99;TRRRRRR5,cAEk::JT      5,lQU.BOOO	     
 52D15LqIII     
 52D15LsKKK      
t*	+	+ P P\,4LMMM 	P 	Pu6qu3OOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	PP P P P P P P P P P P P P P P 
t*	+	+ P P\,4LMMM 	P 	Pu6qu3OOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	PP P P P P P P P P P P P P P P P P Psl   G<4%G%G<%G)	)G<,G)	-G<<H H %J
%I2&J
2I6	6J
9I6	:J

JJc                   ,    e Zd Zd Zd Zd Zd Zd ZdS )Test_ZatValuec                     t           | _        d S N)r   r7   selfs    r8   setup_classzTest_ZatValue.setup_classH   s    


    c           	         t          t          | j        j        ddgt          j        z            ddgd          sJ t          t          | j        j        dt          j        z  dd	gdd
g          ddgd          sJ t          t          | j        j        dt          j        z  dd	ggdd
g          ddggd          sJ dS )zTest broadcast of arguments.r   r   YcB	@gNǣ2?r   r   r   r   r!      r"   r   r    r#   N)r   r   r7   r)   r*   r+   r2   r0   r>   s    r8   test_broadcast_argumentsz&Test_ZatValue.test_broadcast_argumentsK   s    tz~1v~66!34
 
 
 	
 	
 	
 
4quXV	   )$	
 	
 	
 		
 		
 		
 
4qu#hZV	   9%&	
 	
 	
 		
 		
 		
 		
 		
rA   c           	      N   t          t          | j        j        dt          j        z  d          dd          sJ t          t          | j        j        dt          j        z  ddg          dd          sJ t          j        t          d	
          5  t          | j        j        dt          j        z  g d           ddd           n# 1 swxY w Y   t          j        t          d
          5  t          | j        j        dt          j        z  t          j        ddg                     ddd           n# 1 swxY w Y   t          j        ddgg dgt                    }t          t          | j        j        dt          j        z  |          ddgd          sJ dS )z#`bracket` has special requirements.r   N)bracketrC   r   r   r   rD   sequencer%   )r   rD   rD      dtype)r   r   rD   )rK   )r   r   r7   r)   r*   r+   r3   r4   
ValueError	TypeErrornparrayobject)r?   rH   s     r8   test_broadcast_bracketz$Test_ZatValue.test_broadcast_bracketl   sP    tz~q15y$???
 
 
 	
 	
 	
 tz~q15y1a&AAA
 
 
 	
 	
 	
 ]:Z888 	H 	Htz~q15y,,,GGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H ]9G444 	L 	Ltz~q15y"(Aq6:J:JKKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L (QFIII.f===tz~q15y'BBB!34
 
 
 	
 	
 	
 	
 	
s$   ,CCC2>D<<E E c                     t          j        t          d          5  t          | j        j        dt          j        z  g dddg           ddd           dS # 1 swxY w Y   dS )	zShapes mismatch as expected	broadcastr%   r   )r   r!   皙?r   rD   rE   N)r3   r4   rL   r   r7   r2   r*   r0   r>   s    r8   test_bad_broadcastz Test_ZatValue.test_bad_broadcast   s    ]:[999 	 	
4qu"]]V	   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   /AAAc                     t          | j        j        dt          j        z  dd          }t          |t          j                  sJ |j        t          j	        k    sJ |j
        dk    sJ dS )z#Test scalar input returns a scalar.r   r   r   rE    N)r   r7   r2   r*   r0   
isinstanceQuantityrK   rN   float64shape)r?   zs     r8   test_scalar_input_to_outputz)Test_ZatValue.test_scalar_input_to_output   sl    J0$,QQ
 
 
 !QZ(((((w"*$$$$w"}}}}}}rA   N)__name__
__module____qualname__r@   rF   rQ   rU   r]   rW   rA   r8   r;   r;   F   sa          
 
 
B
 
 
@      rA   r;   c                      t          j        t          g d          } t          j        t
          j        d          5   | t          j        dt
          j	        z             ddd           dS # 1 swxY w Y   dS )zTest that numpy vectorize fails on Quantities.

    If this test starts failing then numpy vectorize can be used instead of
    the home-brewed vectorization. Please submit a PR making the change.
    )funcmethodverbose)excludedzdimensionless quantitiesr%   
   N)
rN   	vectorizer   r3   r4   r*   UnitConversionErrorr   r)   r+   )r   s    r8   test_z_at_value_numpyvectorizeri      s     %B%B%B  J 
q,4N	O	O	O - -
8<ae,,,- - - - - - - - - - - - - - - - - -s   $A//A36A3c                    t           }t                      }|                     t          d|           t	          |j        dt          j        z  d          }t          |j	                  |
                                v sJ d S )Nstdoutr   T)rd   )r   r   setattrsysr   r)   r*   r+   strvaluegetvalue)monkeypatchr7   mock_stdoutresxs       r8   test_z_at_value_verbosert      sq    E **KX{333eiQUD999Dtz??k2244444444rA   rc   BrentGoldenBoundedc           	         t           }| dk    rt          j        t          d          5  t	          |j        dt          j        z  |           }ddd           n# 1 swxY w Y   |dk    rd}d	}nd
}d}t          j        t          d          5  t          t	          |j        dt          j        z  | |          |d          sJ 	 ddd           n# 1 swxY w Y   nt          t	          |j        dt          j        z  | d          d
d          sJ t          t	          |j        dt          j        z  | d          dd          sJ t          t	          |j        dt          j        z  | d          d
d          sJ t          t	          |j        dt          j        z  | d          d
d          sJ t          j        t          d          5  t          t	          |j        dt          j        z  | d	          d
d          sJ t          t	          |j        dt          j        z  | d          dd          sJ 	 ddd           n# 1 swxY w Y   t          t	          |j        dt          j        z  | dd          d
d          sJ t          t	          |j        dt          j        z  | d	d          dd          sJ t          j
        t          j                  5  t          j        t          d          5  t	          |j        dt          j        z  | dd           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z
    Test 2 solutions for angular diameter distance by not constraining zmin, zmax,
    but setting `bracket` on the appropriate side of the turning point z.
    Setting zmin / zmax should override `bracket`.
    rx   r$   r%   r   )rc   N皙?r#   )g?      ?g?)rz          @zOption 'bracket' is ignored)rc   rH   r   r   )333333?      ?)r|   r(   )rT   r{   )rT   r~   r|   )rc   rH   r   r{   )rc   rH   r"   )g333333@g      @r(   )r   r3   r6   r   r   r2   r*   r0   UserWarningr   r4   r   r5   )rc   r7   r\   rH   s       r8   test_z_at_value_bracketedr      s    E\,4LMMM 	Y 	Y5:D15LQWXXXA	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Ys77A GGA G\+-KLLL 
	 
	315L!#	   	 	 	 	 	 	 	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 /qu"	   	
 	
 	
 		
 		
 		
 /qu"	   	
 	
 	
 		
 		
 		
 /qu"	   	
 	
 	
 		
 		
 		
 /qu'	   	
 	
 	
 		
 		
 		
 \,4LMMM 	 	315L!&	   	 	 	 	 	 	 315L!&	   	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	* /qu"   

 

 

 
	
 
	
 
	
 /qu"   

 

 

 
	
 
	
 
	
 
t*	+	+  \,4LMMM 	 	/qu"   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                 sl   %AA!A8CCCA/III-M	'L<0M<M 	 MM 	MMMc                 &   t           }ddgddgddgd}| dk    rt          j        t          d          }nt          j        t          d	          }|5  t	          |j        d
t          j        z  dd|           }ddd           n# 1 swxY w Y   |5  t	          |j        d
t          j        z  dd|           }ddd           n# 1 swxY w Y   t          |d||          d                   sJ t          |d||          d
                   sJ dS )z
    Test warnings on non-converged solution when setting `maxfun` to too small iteration number -
    only 'Bounded' returns status value and specific message.
    g-C6?gMbP?g{Gz?rT   ru   rx   z;Solver returned 1: Maximum number of function calls reachedr%   zSolver returned None   r      )r   maxfunrc   N)r"   r   rc   gŊL?r   r   gt)*^ @)	r   r3   r6   r   r   r2   r*   r1   r   )rc   r7   r   ctxz0z1s         r8   test_z_at_value_unconvergedr   8  s    ED\dD\tTlSSDlO
 
 

 l-5KLLL	 
 
+QYQrRX
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
+QYQrRX
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 

 Bd6l1o666666Bd6l1o66666666s$   'BB
B
'CC
C
r7   c           
      6   	 d}d}t           j                                      d          r|dz  }t          j         t          j                  }|D ]_\  }}|                    d          s||v r ||          }t          ||ddgd	
          }t          ||d          sJ d| d            `t           j        t                     rHd	 	fd 	fd 	fdg}|D ]4} ||          }t          |t          ||dd	          d          sJ 3dS dS )zn
    Calculate values from a known redshift, and then check that
    z_at_value returns the right answer.
    r'   )OkOtotangular_diameter_distance_z1z2cloneis_equivalentde_density_scalewWMAP)nu_relative_density)	predicate_r}   r~   g-q=)rH   r   gdy=r   zRound-trip testing z failedr|   c                 0                         |           S r=   )_comoving_distance_z1z2r   r7   z2s    r8   <lambda>z+test_z_at_value_roundtrip.<locals>.<lambda>  s    u44R<< rA   c                 0                         |           S r=   )"_comoving_transverse_distance_z1z2r   s    r8   r   z+test_z_at_value_roundtrip.<locals>.<lambda>  s    u??BGG rA   c                 0                         |           S r=   )r   r   s    r8   r   z+test_z_at_value_roundtrip.<locals>.<lambda>  s    u;;BCC rA   r{   )r   r   N)	rn   name
startswithinspect
getmembersismethodr   r   rX   )
r7   r\   skipmethodsr   rb   fvalgot	func_z1z2r   s
   `        @r8   test_z_at_value_roundtripr   W  s   6 	AD 5:!!&)) )(( '2BCCCG 	Q 	Q
d??3 	44<<tAww
 tc3ZeDDDQU+++PP-P4-P-P-PPPPP %*c"" 	Y<<<<<GGGGGCCCCC
	
  	Y 	YD477DAz$3UKKKRWXXXXXXX	Y 	Y	Y 	YrA   )1r   rm   ior   numpyrN   r3   astropyr   r*   astropy.cosmologyr   r   astropy.cosmology.funcsr   r   astropy.cosmology.realizationsr	   r
   r   r   r   r   r   r   astropy.unitsr   "astropy.utils.compat.optional_depsr   astropy.utils.exceptionsr   markskipifr9   r;   ri   rt   parametrizer   r   	LambdaCDMFlatLambdaCDMwpwaCDMw0wzCDMwCDMFlatwCDMw0waCDMFlatw0waCDMr   rW   rA   r8   <module>r      s}    



                  ( ( ( ( ( ( ( ( B B B B B B B B	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 # " " " " " 8 8 8 8 8 8 7 7 7 7 7 7 	M*?@@&P &P A@&PR 	M*?@@V V V V V V V A@Vr 	M*?@@
- 
- A@
- 	M*?@@5 5 A@5 	M*?@@#A#A#ABBy y CB A@yx 	M*?@@#A#A#ABB7 7 CB A@7: 	M*?@@	! *0Y 0Y+  A@,0Y 0Y 0YrA   