
    IR-e#                         d Z ddlZddlZddlmZ ddlZddlmZ  ee          Z	 edde	j
        z  z
            Z edde	j        z  z             Zd Zd Zd	 Zd
 Zd Zd ZddZd Zd ZddZdS )z}
Miscellaneous utilities for `astropy.units`.

None of the functions in the module are meant for use outside of the
package.
    N)Fraction)finfo      ?g      @c                     t          j        d|           }||                    d          } |                     dd          S )zS
    Get the first sentence from a string and remove any carriage
    returns.
    z	.*?\S\.\sNr   
 )rematchgroupreplace)sxs     3lib/python3.11/site-packages/astropy/units/utils.py_get_first_sentencer      s=    
 	q!!A}GGAJJ99T3    c              #     K   ddl m} g }t                      }|                                 D ]|\  }}t	          ||j                  s||j        k    r't	          ||j                  r+|                    |j	        j
        d         j                   g|                    |           }|                    d            |D ]}t          |j                                                  }d}t	          ||j                  r&d|j	                            d          dd	          d
}d                    d |j        D                       }	||||	|j        |v rdndfV  dS )z
    Generates the ``(unit, doc, represents, aliases, prefixes)``
    tuple used to format the unit summary docs in `generate_unit_summary`.
       )corer   c                 4    | j                                         S N)namelower)r   s    r   <lambda>z$_iter_unit_summary.<locals>.<lambda>>   s    QV\\^^ r   )key z:math:`latex`z, c              3   "   K   | ]
}d | d V  dS )z``N ).0r   s     r   	<genexpr>z%_iter_unit_summary.<locals>.<genexpr>E   s*      ==1JJJJ======r   YesNoN)r   r   setitems
isinstanceUnitBaser   
PrefixUnitadd_representsbasesappendsortr   __doc__stripUnit	to_stringjoinaliases)
	namespacer   unitshas_prefixesr   valunitdoc
representsr4   s
             r   _iter_unit_summaryr<   "   s     
  E55LOO%%  S#t}-- 	 #(??c4?++ 	 S_215:;;;;LL 
JJ++J,,, 
 
!$,//5577
dDI&& 	PO4#3#=#=g#F#Fqt#LOOOJ))======= Y,..EED
 	
 	
 	
 	

 
r   c                     t          j                    }|                    d           d}t          |           D ]}|                     |j        |             |                                S )a`  
    Generates a summary of units from a given namespace.  This is used
    to generate the docstring for the modules that define the actual
    units.

    Parameters
    ----------
    namespace : dict
        A namespace containing units.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    z
.. list-table:: Available Units
   :header-rows: 1
   :widths: 10 20 20 20 1

   * - Unit
     - Description
     - Represents
     - Aliases
     - SI Prefixes
z7
   * - ``{}``
     - {}
     - {}
     - {}
     - {}
)ioStringIOwriter<   formatgetvalue)r5   	docstringtemplateunit_summarys       r   generate_unit_summaryrF   P   sz      IOO
	  H +955 8 867777r   c                 N   ddl m} i }|                                 D ]1\  }}t          ||          r|j        j        d         }|||j        <   2t          j                    }d}t          |          D ]}|
                     |j        |             |                                S )a  
    Generates table entries for units in a namespace that are just prefixes
    without the base unit.  Note that this is intended to be used *after*
    `generate_unit_summary` and therefore does not include the table header.

    Parameters
    ----------
    namespace : dict
        A namespace containing units that are prefixes but do *not* have the
        base unit in their namespace.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    r   )r)   r   zO
   * - Prefixes for ``{}``
     - {} prefixes
     - {}
     - {}
     - Only
)r   r)   r&   r'   r;   r,   r   r>   r?   r<   r@   rA   rB   )	r5   r)   faux_namespacenmr9   	base_unitrC   rD   rE   s	            r    generate_prefixonly_unit_summaryrK   |   s    " NOO%% 7 7DdJ'' 	7-a0I-6N9>*IH +>:: 8 867777r   c                     	 t           | cxk    o
t          k    nc S # t          $ rD t           | j        cxk    o
t          k    nc o!t           | j        dz   cxk    o
t          k    nc cY S w xY w)Nr   )_JUST_BELOW_UNITY_JUST_ABOVE_UNITY	TypeErrorrealimag)values    r   is_effectively_unityrS      s    
 E>>>>->>>>>> 
 
 
@@@@/@@@@@ I!UZ!^HHHH7HHHHH	
 	
 	

s    AA*)A*c                    t          |           rdS | j        t          u r| S t          | d          r|                                 } | j        rt          | j                  t          | j                  k    r't          | j        | j        z  dz             r| j        S n4t          | j        | j        z  dz             rt          d| j                  S | S | j        S )Nr   dtyper   g        )	rS   	__class__floathasattritemrQ   absrP   complex)scales    r   sanitize_scaler]      s    E"" s % ug 

 z uz??S__,,#EJ$;a$?@@ "z!" "%*uz"9A"=>> 	,3
+++ zr   d   c                 :   | dk    s| j         t          u r| S |                                 \  }}||z  }d\  }}|d}}||k    rYt          ||| z  z  cxk    rt          k    rn nt          ||          S ||||z  z
  }}||z  }||||z  z   }}||||z  z   }}||k    Y| S )aS  Fraction very close to x with denominator at most max_denominator.

    The fraction has to be such that fraction/x is unity to within 4 ulp.
    If such a fraction does not exist, returns the float number.

    The algorithm is that of `fractions.Fraction.limit_denominator`, but
    sped up by not creating a fraction to start with.
    r   )r   r   r   )rV   intas_integer_ratiorM   rN   r   )	pmax_denominatorndan0d0n1d1s	            r   maybe_simple_fractionrk      s     	Avv##DAq	QAFBB


b1fBBBB1BBBBBBB###!a!e)1FR!b&[BR!b&[B 

 Hr   c                    t          | dd          }|	 t          |           } ny# t          $ rl 	 t          j        |           } | j        d         x}| k                                    rt          |          } nt          d          n# t          $ r  w xY wY nw xY wt          |           } t          | t                    r| S | j
        }|dk    r| j        } n||dz
  z  dk    rt          |           } | S )a  Convert a power to a floating point value, an integer, or a Fraction.

    If a fractional power can be represented exactly as a floating point
    number, convert it to a float, to make the math much faster; otherwise,
    retain it as a `fractions.Fraction` object to avoid losing precision.
    Conversely, if the value is indistinguishable from a rational number with a
    low-numbered denominator, convert to a Fraction object.

    Parameters
    ----------
    p : float, int, Rational, Fraction
        Power to be converted
    denominatorNr   z9Quantities and Units may only be raised to a scalar powerr   )getattrrW   	Exceptionnp
asanyarrayflatall
ValueErrorrk   r'   rm   	numerator)rb   denomfirsts      r   validate_powerrx      s4    A}d++E}	aAA 
	 
	 
		M!$$fQi'UA-2244 eAA$S   
     A	
	 "!$$a 	H zzK
519
!	#	# !HHHs'   % 
BAB	B	BBBc                    | j         t          uo"| j         t          uot          | t                    }|j         t          uo"|j         t          uot          |t                    }|r|st          |          }n|s|rt          |           } | |fS )z
    If either input is a Fraction, convert the other to a Fraction
    (at least if it does not have a ridiculous denominator).
    This ensures that any operation involving a Fraction will use
    rational arithmetic and preserve precision.
    )rV   r`   rW   r'   r   rk   )rf   ba_is_fractionb_is_fractions       r   resolve_fractionsr}     s     	
3W1;e#;W
1h@W@W  	
3W1;e#;W
1h@W@W   %] %!!$$ %} %!!$$a4Kr   c                     ddl m t          | t          j                  s<t          j        |           s(t          fd| D                       r | |          S |t          j        u rt          j        n|}t          j	        | |          S )Nr   )Quantityc              3   8   K   | ]}t          |          V  d S r   )r'   )r!   r   r   s     r   r"   z&quantity_asanyarray.<locals>.<genexpr>5  s-      33A
1h''333333r   )rU   )
quantityr   r'   rp   ndarrayisscalaranyinexactfloat64rq   )rf   rU   r   s     @r   quantity_asanyarrayr   /  s    """""" q"*%%	-A	- 333333333	-
 x'''' $rz11

u}Qe,,,,r   )r^   r   )r/   r>   r	   	fractionsr   numpyrp   r   rW   _float_finfoepsnegrM   epsrN   r   r<   rF   rK   rS   r]   rk   rx   r}   r   r    r   r   <module>r      sJ    
			 				                uU|| E#l&9 99:: E#l&6 6677      +
 +
 +
\)  )  ) X$  $  $ N	
 	
 	
  :   6/ / /d  ,- - - - - -r   