a
    ߙfb!                     @   s   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edde	j  Zdd Zd	d
 Zdd Zdd Zdd Zdd ZdddZdd Zdd Zd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                 C   s*   t d| }|dur|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x r   2lib/python3.9/site-packages/astropy/units/utils.py_get_first_sentence   s    
r   c           
      c   s   ddl m} g }t }|  D ]N\}}t||js4q||jkr@qt||jrb||j	j
d j q|| q|jdd d |D ]r}t|j }d}t||jrd|j	d	dd
  d}ddd |jD }	||||	|j|v rdndfV  qdS )z
    Generates the ``(unit, doc, represents, aliases, prefixes)``
    tuple used to format the unit summary docs in `generate_unit_summary`.
       )corer   c                 S   s
   | j  S N)namelower)r   r   r   r   <lambda>A       z$_iter_unit_summary.<locals>.<lambda>)key z:math:`Zlatex`z, c                 s   s   | ]}d | d V  qdS )z``Nr   .0r   r   r   r   	<genexpr>H   r   z%_iter_unit_summary.<locals>.<genexpr>ZYesZNoN)r   r   setitems
isinstanceZUnitBaser   
PrefixUnitaddZ_representsbasesappendsortr   __doc__stripZUnitZ	to_stringjoinaliases)
	namespacer   ZunitsZhas_prefixesr   valunitdoc
representsr)   r   r   r   _iter_unit_summary$   s&    
r/   c                 C   s8   t  }|d t| D ]}|dj|  q| 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)r*   	docstringunit_summaryr   r   r   generate_unit_summaryM   s    
r7   c                 C   sp   ddl m} i }|  D ](\}}t||r|jjd }|||j< qt }t	|D ]}|
dj|  qR| 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   r0   r1   r/   r2   r3   r4   )r*   r!   Zfaux_namespaceZnmr,   Z	base_unitr5   r6   r   r   r    generate_prefixonly_unit_summaryx   s    
r8   c                 C   sj   zt |   kotkn  W S  tyd   t | j  ko<tkn  o^t | jd   koZtkn   Y S 0 d S )Nr   )_JUST_BELOW_UNITY_JUST_ABOVE_UNITY	TypeErrorrealimag)valuer   r   r   is_effectively_unity   s    r?   c                 C   s   t | rdS | jtu r| S t| dr,|  } | jrt| jt| jkrbt | j| j d r| jS n t | j| j d rtd| jS | S | jS d S )Nr   dtyper   g        )	r?   	__class__floathasattritemr=   absr<   complex)Zscaler   r   r   sanitize_scale   s    

rG   d   c           	      C   s   | dks| j tu r| S |  \}}|| }d\}}|d }}||krt|||     kr`tkrnn n
t||S ||||   }}|| }||||   }}||||   }}q<| 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   )rA   intas_integer_ratior9   r:   r   )	pZmax_denominatorndaZn0Zd0Zn1Zd1r   r   r   maybe_simple_fraction   s    	
 
rO   c                 C   s   t | dd}|du rfzt| } W n( tyH   t| sBtdn Y n0 t| } t| tr`| S | j}|dkrv| j	} n||d @ dkrt| } | 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
    denominatorNz9Quantities and Units may only be raised to a scalar powerr   r   )
getattrrB   	Exceptionnpisscalar
ValueErrorrO   r    rP   	numerator)rK   Zdenomr   r   r   validate_power   s"    


rW   c                 C   sf   | j tuo| j tuot| t}|j tuo:|j tuo:t|t}|rN|sNt|}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.
    )rA   rI   rB   r    r   rO   )rN   bZa_is_fractionZb_is_fractionr   r   r   resolve_fractions  s    	
rY   c                    sV   ddl m  t| tjsDt| sDt fdd| D rD | |dS tj| |dS d S )Nr   Quantityc                 3   s   | ]}t | V  qd S r   )r    r   rZ   r   r   r   $  r   z&quantity_asanyarray.<locals>.<genexpr>)r@   )Zquantityr[   r    rS   ZndarrayrT   anyZ
asanyarray)rN   r@   r   rZ   r   quantity_asanyarray"  s    ,r]   )rH   )N)r&   r0   r   Z	fractionsr   ZnumpyrS   r   rB   Z_float_finfoZepsnegr9   Zepsr:   r   r/   r7   r8   r?   rG   rO   rW   rY   r]   r   r   r   r   <module>   s$   )+'

-