U
    e»{j#  ã                   @   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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   úR/var/www/html/bwave/python/venv/lib/python3.8/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ƒrÀd|j	 d	¡dd
… › d�}d dd„ |jD ƒ¡}	||||	|j|krìdndfV  q‚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>@   ó    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>G   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#   s0    
ûr0   c                 C   s<   t  ¡ }| d¡ d}t| ƒ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Úwriter0   ÚformatÚgetvalue)r+   Ú	docstringÚtemplateÚunit_summaryr   r   r   Úgenerate_unit_summaryR   s    ÿr9   c           	      C   st   ddl m} i }|  ¡ D ](\}}t||ƒr|jjd }|||j< qt ¡ }d}t	|ƒD ]}| 
|j|Ž ¡ qV| ¡ 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   r1   r2   r0   r3   r4   r5   )	r+   r"   Zfaux_namespaceÚnmr-   Z	base_unitr6   r7   r8   r   r   r   Ú generate_prefixonly_unit_summary   s    
r;   c                 C   sl   zt |   kotkn  W S  tk
rf   t | j  ko>tkn  o`t | jd   ko\tkn   Y S X d S )Nr   )Ú_JUST_BELOW_UNITYÚ_JUST_ABOVE_UNITYÚ	TypeErrorÚrealÚimag)Úvaluer   r   r   Úis_effectively_unity¦   s    þrB   c                 C   s�   t | ƒrdS | jtkr| S t| dƒr,|  ¡ } | jr†t| jƒt| jƒkrbt | j| j d ƒr‚| jS n t | j| j d ƒr‚td| jƒS | S | jS d S )Nr   Údtyper   g        )	rB   Ú	__class__ÚfloatÚhasattrÚitemr@   Úabsr?   Úcomplex)Úscaler   r   r   Úsanitize_scale²   s    

rK   éd   c           	      C   s²   | dks| j tkr| S |  ¡ \}}|| }d\}}|d }}||kr®t|||     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   )rD   ÚintÚas_integer_ratior<   r=   r   )	ÚpÚmax_denominatorÚnÚdÚaZn0Zd0Zn1Zd1r   r   r   Úmaybe_simple_fractionÏ   s    	
 
rT   c                 C   s”   t | ddƒ}|dkrhz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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
    ÚdenominatorNz9Quantities and Units may only be raised to a scalar powerr   r   )
ÚgetattrrE   Ú	ExceptionÚnpÚisscalarÚ
ValueErrorrT   r!   rU   Ú	numerator)rO   Údenomr   r   r   Úvalidate_powerê   s&    
ÿ
r]   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.
    )rD   rM   rE   r!   r   rT   )rS   ÚbZa_is_fractionZb_is_fractionr   r   r   Úresolve_fractions  s    
ÿÿ
r_   c                    sj   ddl m‰  t| tjƒsDt | ¡sDt‡ fdd„| D ƒƒrDˆ | |d�S |tjkrTtjn|}tj	| |d�S d S )Nr   ©ÚQuantityc                 3   s   | ]}t |ˆ ƒV  qd S r   )r!   r   r`   r   r   r   4  s     z&quantity_asanyarray.<locals>.<genexpr>)rC   )
Zquantityra   r!   rX   ZndarrayrY   ÚanyZinexactZfloat64Z
asanyarray)rS   rC   r   r`   r   Úquantity_asanyarray.  s    
ÿþýrc   )rL   )N)r'   r1   r   Ú	fractionsr   ÚnumpyrX   r   rE   Z_float_finfoZepsnegr<   Úepsr=   r   r0   r9   r;   rB   rK   rT   r]   r_   rc   r   r   r   r   Ú<module>   s$   /-'
.