B
    d!                 @   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)finfog      ?g      @c             C   s*   t d| }|dk	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   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/units/utils.py_get_first_sentence   s    
r   c       
      c   s  ddl m} g }t }xZ|  D ]N\}}t||js6q ||jkrBq t||jrd||j	j
d j q || q W |jdd d xz|D ]r}t|j }d}t||jrd|j	d	dd
  d}ddd |jD }	||||	|j|krdndfV  qW d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:`latex`z, c             s   s   | ]}d | d V  qdS )z``Nr   ).0r   r   r   r   	<genexpr>H   s    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   s<   t  }|d x t| D ]}|dj|  qW | 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    
r5   c             C   sx   ddl m} i }x4|  D ](\}}t||r|jjd }|||j< qW t }x t	|D ]}|
dj|  qXW | 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-   r0   r1   r2   )r(   r   Zfaux_namespacenmr*   Z	base_unitr3   r4   r   r   r    generate_prefixonly_unit_summaryx   s    
r7   c             C   sf   yt |   kotkS   S  tk
r`   t | j  ko<tkn  o^t | jd   koZtkS   S X 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k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 )Ng      ?dtyper   g        )	r>   	__class__floathasattritemr<   absr;   complex)scaler   r   r   sanitize_scale   s    

rG   d   c       	      C   s   | dks| j tkr| S |  \}}|| }d\}}|d }}xt||krt|||     krbtkrpn n
t||S ||||   }}|| }||||   }}||||   }}q>W | 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   )r@   intas_integer_ratior8   r9   r   )	pmax_denominatorndaZn0Zd0Zn1Zd1r   r   r   maybe_simple_fraction   s    	

 
rP   c             C   s   t | dd}|dkrhyt| } W n* tk
rJ   t| sDtdn Y nX t| } t| trb| S | j}|dkrx| 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   )
getattrrA   	Exceptionnpisscalar
ValueErrorrP   r   rQ   	numerator)rK   denomr   r   r   validate_power   s"    


rY   c             C   sf   | j tk	o| j tk	ot| t}|j tk	o:|j tk	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.
    )r@   rI   rA   r   r   rP   )rO   bZa_is_fractionZb_is_fractionr   r   r   resolve_fractions  s    	


r[   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 )N)r   )r   r   )r\   r   r   r   $  s    z&quantity_asanyarray.<locals>.<genexpr>)r?   )Zquantityr\   r   rT   ZndarrayrU   anyZ
asanyarray)rO   r?   r   )r\   r   quantity_asanyarray"  s    ,r^   )rH   )N)r$   r.   r   	fractionsr   numpyrT   r   rA   Z_float_finfoZepsnegr8   Zepsr9   r   r-   r5   r7   r>   rG   rP   rY   r[   r^   r   r   r   r   <module>   s$   )+'

-