ó
šßÈ[c           @  s‘   d  d l  m Z m Z d  d l Z d  d l m Z d  d l Z d d l m	 Z	 m
 Z
 d „  Z d „  Z d d d „ Z d	 d
 d e d d „ Z d S(   iÿÿÿÿ(   t   print_functiont   divisionN(   t	   factoriali   (   t   ranget   mapc         C  s¢   t  t j d ƒ r( t j j |  | | ƒ St j d ƒ t j |  ƒ }  t j | | ƒ \ } } t j	 | ƒ } |  | c g  | D] } | | | k j
 ƒ  ^ q| 7<d S(   s¤   Utility that computes np.add.at()

    The fast version is available only in Numpy 1.8+; for older versions of
    numpy this defaults to a slower computation.
    t   atsX   Using slow replacement for numpy.add.at(). For ~100x faster results update to numpy 1.8+N(   t   hasattrt   npt   ufunct   addR   t   warningst   warnt   asarrayt   broadcast_arrayst   uniquet   sum(   t   arrt   indt   valst   unqt   i(    (    sN   lib/python2.7/site-packages/astropy/stats/lombscargle/implementations/utils.pyt   add_at	   s    c         C  st   t  t d ƒ r' d t |  d ƒ j ƒ  >St |  ƒ d }  x. d d d d d d g D] } |  |  | ?O}  qP W|  d Sd S(	   s·   
    Find the bit (i.e. power of 2) immediately greater than or equal to N
    Note: this works for numbers up to 2 ** 64.
    Roughly equivalent to int(2 ** np.ceil(np.log2(N)))
    t
   bit_lengthi   i   i   i   i   i    N(   R   t   intR   (   t   NR   (    (    sN   lib/python2.7/site-packages/astropy/stats/lombscargle/implementations/utils.pyt   bitceil   s    c         C  s’  t  t j t j |  | ƒ ƒ \ }  } | d k rT t t j |  ƒ d | d ƒ } n  t j | d | j ƒ} |  d d k } t	 | |  | j
 t ƒ | | ƒ |  | | | }  } t j |  | d j
 t ƒ d | | ƒ } | t j |  | t j | ƒ d d … t j f d ƒ } t | d ƒ } xc t | ƒ D]U }	 |	 d k r\| |	 |	 | 9} n  | | d |	 }
 t	 | |
 | | |  |
 ƒ q5W| S(   sò  
    Extirpolate the values (x, y) onto an integer grid range(N),
    using lagrange polynomial weights on the M nearest points.
    Parameters
    ----------
    x : array_like
        array of abscissas
    y : array_like
        array of ordinates
    N : int
        number of integer bins to use. For best performance, N should be larger
        than the maximum of x
    M : int
        number of adjoining points on which to extirpolate.

    Returns
    -------
    yN : ndarray
         N extirpolated values associated with range(N)

    Example
    -------
    >>> rng = np.random.RandomState(0)
    >>> x = 100 * rng.rand(20)
    >>> y = np.sin(x)
    >>> y_hat = extirpolate(x, y)
    >>> x_hat = np.arange(len(y_hat))
    >>> f = lambda x: np.sin(x / 10)
    >>> np.allclose(np.sum(y * f(x)), np.sum(y_hat * f(x_hat)))
    True

    Notes
    -----
    This code is based on the C implementation of spread() presented in
    Numerical Recipes in C, Second Edition (Press et al. 1989; p.583).
    g      à?i   t   dtypei    i   N(   R   R   t   ravelR   t   NoneR   t   maxt   zerosR   R   t   astypet   clipt   prodt   aranget   newaxisR   R   (   t   xt   yR   t   Mt   resultt   integerst   ilot	   numeratort   denominatort   jR   (    (    sN   lib/python2.7/site-packages/astropy/stats/lombscargle/implementations/utils.pyt   extirpolate*   s     %$$!*: i    i   i   c	         C  s  | | 9} | | 9} | d k r/ t  d ƒ ‚ n  t t j t j |  | ƒ ƒ \ }  } | rwt | ƒ } | d k r€ t  d ƒ ‚ n  t | | ƒ }	 |  j ƒ  }
 | d k rÑ | t j d t j	 | |  |
 ƒ } n  |  |
 |	 | |	 } t
 | | |	 | ƒ } t j j | ƒ |  } |
 d k rZ| | t j | ƒ } | t j d t j	 |
 | ƒ 9} n  |	 | j } |	 | j } n‘ | | t j | ƒ } t j | t j d t j	 | |  d d … t j f ƒ ƒ } t j | t j d t j	 | |  d d … t j f ƒ ƒ } | | f S(   sè  Compute (approximate) trigonometric sums for a number of frequencies
    This routine computes weighted sine and cosine sums:
        S_j = sum_i { h_i * sin(2 pi * f_j * t_i) }
        C_j = sum_i { h_i * cos(2 pi * f_j * t_i) }
    Where f_j = freq_factor * (f0 + j * df) for the values j in 1 ... N.
    The sums can be computed either by a brute force O[N^2] method, or
    by an FFT-based O[Nlog(N)] method.

    Parameters
    ----------
    t : array_like
        array of input times
    h : array_like
        array weights for the sum
    df : float
        frequency spacing
    N : int
        number of frequency bins to return
    f0 : float (optional, default=0)
        The low frequency to use
    freq_factor : float (optional, default=1)
        Factor which multiplies the frequency
    use_fft : bool
        if True, use the approximate FFT algorithm to compute the result.
        This uses the FFT with Press & Rybicki's Lagrangian extirpolation.
    oversampling : int (default = 5)
        oversampling freq_factor for the approximation; roughly the number of
        time samples across the highest-frequency sinusoid. This parameter
        contains the tradeoff between accuracy and speed. Not referenced
        if use_fft is False.
    Mfft : int
        The number of adjacent points to use in the FFT approximation.
        Not referenced if use_fft is False.

    Returns
    -------
    S, C : ndarrays
        summation arrays for frequencies f = df * np.arange(1, N + 1)
    i    s   df must be positives   Mfft must be positivey               @i   N(   t
   ValueErrorR   R   R   R   R   R   t   mint   expt   piR-   t   fftt   ifftR"   t   realt   imagt   dott   cosR#   t   sin(   t   tt   ht   dfR   t   f0t   freq_factort   oversamplingt   use_fftt   Mfftt   Nfftt   t0t   tnormt   gridt   fftgridt   ft   Ct   S(    (    sN   lib/python2.7/site-packages/astropy/stats/lombscargle/implementations/utils.pyt   trig_suml   s2    )

$)%==(   t
   __future__R    R   R
   t   mathR   t   numpyR   t   extern.six.movesR   R   R   R   R   R-   t   TrueRI   (    (    (    sN   lib/python2.7/site-packages/astropy/stats/lombscargle/implementations/utils.pyt   <module>   s   		B