B
    &]\U              	   @   s  d Z ddlmZmZmZ ddddddd	d
gZddlmZmZ ddlZddl	m
Z
 ddlmZ ddlZee
j ee
j ee
j ee
j ee
j ee
j [dd Zdd Zdd Zdd Zdd Zdd Zdd Zeejeeeje
jeej eeej!e
j"iZ#eejeeeje
j$iZ%eej eeej!e
j&eejeeeje
j&iZ'dd Z(d d! Z)d"d# Z*d3d&dZ+d4d'dZ,d5d(dZ-d6d)dZ.d*d+ Z/d7d,dZ0d-d. Z1d8d/dZ2d9d1d	Z3d:d2d
Z4dS );z(
Discrete Fourier Transforms - basic.py
    )divisionprint_functionabsolute_importfftifftfftnifftnrfftirfftfft2ifft2)swapaxeszerosN   )_fftpack)_init_nd_shape_and_axes_sortedc             C   s   t | jj|S )N)
issubclassdtypetype)arrZ	typeclass r   2lib/python3.7/site-packages/scipy/fftpack/basic.pyistype   s    r   c             C   s0   | |krdS t |tjs&t|dr&dS | jdkS )z}
    Strict check for `arr` not sharing any data with `original`,
    under the assumption that arr = asarray(original)

    FZ	__array__N)
isinstancenumpyZndarrayhasattrbase)r   Zoriginalr   r   r   _datacopied   s
    r   c             C   sJ   t | } | dkrdS x&dD ]}x| | dkr6| | } q W qW | | d @  S )z
    Is the size of FFT such that FFTPACK can handle it in single precision
    with sufficient accuracy?

    Composite numbers of 2, 3, and 5 are accepted, as FFTPACK has those
    r   T)      r   )int)ncr   r   r   _is_safe_size3   s    
r#   c             O   s<   t |rtj| |f||S tj| |f||tjS d S )N)r#   r   Zcrfftzrfftastyper   	complex64)xr!   akwr   r   r   _fake_crfftH   s    r*   c             O   s<   t |rtj| |f||S tj| |f||tjS d S )N)r#   r   Zcfftzfftr%   r   r&   )r'   r!   r(   r)   r   r   r   
_fake_cfftO   s    r,   c             O   s<   t |rtj| |f||S tj| |f||tjS d S )N)r#   r   r	   drfftr%   r   float32)r'   r!   r(   r)   r   r   r   
_fake_rfftV   s    r/   c             O   sH   t ttt|r(tj| |f||S tj| |f||t j	S d S )N)
r   alllistmapr#   r   Zcfftndzfftndr%   r&   )r'   shaper(   r)   r   r   r   _fake_cfftnd]   s    r5   c             C   s   t | drH| jjtjd krH| jtjkr8tj| tjdS tj| | jdS t| }|jtjkrntj|tjdS |jjtjd krt| S |S dS )zLike numpy asfarray, except that it does not modify x dtype if x is
    already an array with a float dtype, and do not cast complex types to
    real.r   ZAllFloat)r   N)	r   r   charr   Z	typecodesZhalfZasarrayr.   Zasfarray)r'   Zretr   r   r   	_asfarray}   s    

r7   c             C   s   t | j}|| |krJtdgt| }td|||< | t| } | dfS tdgt| }td|| ||< |||< t|| jj}| |t|< |dfS dS )z6 Internal auxiliary function for _raw_fft, _raw_fftnd.Nr   FT)r1   r4   slicelentupler   r   r6   )r'   r!   axissindexzr   r   r   
_fix_shape   s    
r?   c             C   s   |dkr| j | }n&|| j | kr:t| ||\} }|p8|}|dk rNtd| |dksh|t| j d krz|| |||d}n(t| |d} || |||d}t||d}|S )z8 Internal auxiliary function for fft, ifft, rfft, irfft.Nr   z1Invalid number of FFT data points (%d) specified.)overwrite_x)r4   r?   
ValueErrorr9   r   )r'   r!   r;   	directionrA   work_function	copy_maderr   r   r   _raw_fft   s    rG   r@   Fc             C   s  t | }yt|j }W n" tk
r8   td|j Y nX t|tjsVt|tjsVd}|pbt	|| }|dkrx|j
| }n&||j
| krt|||\}}|p|}|dk rtd| |dks|t|j
d kr|||dd|S t||d}|||dd|}t||dS )a  
    Return discrete Fourier transform of real or complex sequence.

    The returned complex array contains ``y(0), y(1),..., y(n-1)`` where

    ``y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum()``.

    Parameters
    ----------
    x : array_like
        Array to Fourier transform.
    n : int, optional
        Length of the Fourier transform.  If ``n < x.shape[axis]``, `x` is
        truncated.  If ``n > x.shape[axis]``, `x` is zero-padded. The
        default results in ``n = x.shape[axis]``.
    axis : int, optional
        Axis along which the fft's are computed; the default is over the
        last axis (i.e., ``axis=-1``).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    z : complex ndarray
        with the elements::

            [y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)]        if n is even
            [y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)]  if n is odd

        where::

            y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1

    See Also
    --------
    ifft : Inverse FFT
    rfft : FFT of a real sequence

    Notes
    -----
    The packing of the result is "standard": If ``A = fft(a, n)``, then
    ``A[0]`` contains the zero-frequency term, ``A[1:n/2]`` contains the
    positive-frequency terms, and ``A[n/2:]`` contains the negative-frequency
    terms, in order of decreasingly negative frequency. So for an 8-point
    transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1].
    To rearrange the fft output so that the zero-frequency component is
    centered, like [-4, -3, -2, -1,  0,  1,  2,  3], use `fftshift`.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    This function is most efficient when `n` is a power of two, and least
    efficient when `n` is prime.

    Note that if ``x`` is real-valued then ``A[j] == A[n-j].conjugate()``.
    If ``x`` is real-valued and ``n`` is even then ``A[n/2]`` is real.

    If the data type of `x` is real, a "real FFT" algorithm is automatically
    used, which roughly halves the computation time.  To increase efficiency
    a little further, use `rfft`, which does the same calculation, but only
    outputs half of the symmetrical spectrum.  If the data is both real and
    symmetrical, the `dct` can again double the efficiency, by generating
    half of the spectrum from half of the signal.

    Examples
    --------
    >>> from scipy.fftpack import fft, ifft
    >>> x = np.arange(5)
    >>> np.allclose(fft(ifft(x)), x, atol=1e-15)  # within numerical accuracy.
    True

    ztype %s is not supportedr   Nz1Invalid number of FFT data points (%d) specified.r@   r   )r7   _DTYPE_TO_FFTr   KeyErrorrB   r   r   r&   
complex128r   r4   r?   r9   r   )r'   r!   r;   rA   tmprD   rE   r   r   r   r      s*    Kc             C   s  t | }yt|j }W n" tk
r8   td|j Y nX t|tjsVt|tjsVd}|pbt	|| }|dkrx|j
| }n&||j
| krt|||\}}|p|}|dk rtd| |dks|t|j
d kr|||dd|S t||d}|||dd|}t||dS )aw  
    Return discrete inverse Fourier transform of real or complex sequence.

    The returned complex array contains ``y(0), y(1),..., y(n-1)`` where

    ``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``.

    Parameters
    ----------
    x : array_like
        Transformed data to invert.
    n : int, optional
        Length of the inverse Fourier transform.  If ``n < x.shape[axis]``,
        `x` is truncated.  If ``n > x.shape[axis]``, `x` is zero-padded.
        The default results in ``n = x.shape[axis]``.
    axis : int, optional
        Axis along which the ifft's are computed; the default is over the
        last axis (i.e., ``axis=-1``).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    ifft : ndarray of floats
        The inverse discrete Fourier transform.

    See Also
    --------
    fft : Forward FFT

    Notes
    -----
    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    This function is most efficient when `n` is a power of two, and least
    efficient when `n` is prime.

    If the data type of `x` is real, a "real IFFT" algorithm is automatically
    used, which roughly halves the computation time.

    Examples
    --------
    >>> from scipy.fftpack import fft, ifft
    >>> import numpy as np
    >>> x = np.arange(5)
    >>> np.allclose(ifft(fft(x)), x, atol=1e-15)  # within numerical accuracy.
    True

    ztype %s is not supportedr   Nz1Invalid number of FFT data points (%d) specified.r@   )r7   rH   r   rI   rB   r   r   r&   rJ   r   r4   r?   r9   r   )r'   r!   r;   rA   rK   rD   rE   r   r   r   r   #  s*    5c             C   sl   t | }t|stdyt|j }W n" tk
rJ   td|j Y nX |pXt|| }t	|||d||S )a  
    Discrete Fourier transform of a real sequence.

    Parameters
    ----------
    x : array_like, real-valued
        The data to transform.
    n : int, optional
        Defines the length of the Fourier transform.  If `n` is not specified
        (the default) then ``n = x.shape[axis]``.  If ``n < x.shape[axis]``,
        `x` is truncated, if ``n > x.shape[axis]``, `x` is zero-padded.
    axis : int, optional
        The axis along which the transform is applied.  The default is the
        last axis.
    overwrite_x : bool, optional
        If set to true, the contents of `x` can be overwritten. Default is
        False.

    Returns
    -------
    z : real ndarray
        The returned real array contains::

          [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))]              if n is even
          [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))]   if n is odd

        where::

          y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n)
          j = 0..n-1

    See Also
    --------
    fft, irfft, numpy.fft.rfft

    Notes
    -----
    Within numerical accuracy, ``y == rfft(irfft(y))``.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    To get an output with a complex datatype, consider using the related
    function `numpy.fft.rfft`.

    Examples
    --------
    >>> from scipy.fftpack import fft, rfft
    >>> a = [9, -9, 1, 3]
    >>> fft(a)
    array([  4. +0.j,   8.+12.j,  16. +0.j,   8.-12.j])
    >>> rfft(a)
    array([  4.,   8.,  12.,  16.])

    z"1st argument must be real sequenceztype %s is not supportedr   )
r7   r   	isrealobj	TypeError_DTYPE_TO_RFFTr   rI   rB   r   rG   )r'   r!   r;   rA   rK   rD   r   r   r   r	   v  s    :
c             C   sl   t | }t|stdyt|j }W n" tk
rJ   td|j Y nX |pXt|| }t	|||d||S )aM  
    Return inverse discrete Fourier transform of real sequence x.

    The contents of `x` are interpreted as the output of the `rfft`
    function.

    Parameters
    ----------
    x : array_like
        Transformed data to invert.
    n : int, optional
        Length of the inverse Fourier transform.
        If n < x.shape[axis], x is truncated.
        If n > x.shape[axis], x is zero-padded.
        The default results in n = x.shape[axis].
    axis : int, optional
        Axis along which the ifft's are computed; the default is over
        the last axis (i.e., axis=-1).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    irfft : ndarray of floats
        The inverse discrete Fourier transform.

    See Also
    --------
    rfft, ifft, numpy.fft.irfft

    Notes
    -----
    The returned real array contains::

        [y(0),y(1),...,y(n-1)]

    where for n is even::

        y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k])
                                     * exp(sqrt(-1)*j*k* 2*pi/n)
                    + c.c. + x[0] + (-1)**(j) x[n-1])

    and for n is odd::

        y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k])
                                     * exp(sqrt(-1)*j*k* 2*pi/n)
                    + c.c. + x[0])

    c.c. denotes complex conjugate of preceding expression.

    For details on input parameters, see `rfft`.

    To process (conjugate-symmetric) frequency-domain data with a complex
    datatype, consider using the related function `numpy.fft.irfft`.

    Examples
    --------
    >>> from scipy.fftpack import rfft, irfft
    >>> a = [1.0, 2.0, 3.0, 4.0, 5.0]
    >>> irfft(a)
    array([ 2.6       , -3.16405192,  1.24398433, -1.14955713,  1.46962473])
    >>> irfft(rfft(a))
    array([1., 2., 3., 4., 5.])

    z"1st argument must be real sequenceztype %s is not supportedr@   )
r7   r   rL   rM   rN   r   rI   rB   r   rG   )r'   r!   r;   rA   rK   rD   r   r   r   r
     s    B
c             C   s.  |dk}t | ||\}}|rVx(|D ] }t| || |\} }|p@|}q"W || |||dS x.td|jd D ]}	t| ||	  |	 } qhW tt| j|j | j}
t| j}|||
< x4tt	|
D ]$}	t| ||	 |
|	 \} }|p|}qW || |||d}x0tt	|ddD ]}	t||	 ||	  }q
W |S )z.Internal auxiliary function for fftnd, ifftnd.N)rA   r   r   r@   )
r   r?   rangesizer   r   r1   ndimZonesr9   )r'   r<   axesrC   rA   rD   ZnoaxesZaxrE   iZwaxesr4   rF   r   r   r   
_raw_fftnd  s&    
rT   c             C   s   t | |||dS )a  
    Return multidimensional discrete Fourier transform.

    The returned array contains::

      y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
         x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i)

    where d = len(x.shape) and n = x.shape.

    Parameters
    ----------
    x : array_like
        The (n-dimensional) array to transform.
    shape : int or array_like of ints or None, optional
        The shape of the result.  If both `shape` and `axes` (see below) are
        None, `shape` is ``x.shape``; if `shape` is None but `axes` is
        not None, then `shape` is ``scipy.take(x.shape, axes, axis=0)``.
        If ``shape[i] > x.shape[i]``, the i-th dimension is padded with zeros.
        If ``shape[i] < x.shape[i]``, the i-th dimension is truncated to
        length ``shape[i]``.
        If any element of `shape` is -1, the size of the corresponding
        dimension of `x` is used.
    axes : int or array_like of ints or None, optional
        The axes of `x` (`y` if `shape` is not None) along which the
        transform is applied.
        The default is over all axes.
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed.  Default is False.

    Returns
    -------
    y : complex-valued n-dimensional numpy array
        The (n-dimensional) DFT of the input array.

    See Also
    --------
    ifftn

    Notes
    -----
    If ``x`` is real-valued, then
    ``y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate()``.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    Examples
    --------
    >>> from scipy.fftpack import fftn, ifftn
    >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
    >>> np.allclose(y, fftn(ifftn(y)))
    True

    r   )_raw_fftn_dispatch)r'   r4   rR   rA   r   r   r   r   6  s    :c             C   sv   t | }yt|j }W n" tk
r8   td|j Y nX t|tjsVt|tjsVd}|pbt	|| }t
||||||S )Nztype %s is not supportedr   )r7   _DTYPE_TO_FFTNr   rI   rB   r   r   r&   rJ   r   rT   )r'   r4   rR   rA   rC   rK   rD   r   r   r   rU   s  s    rU   c             C   s   t | |||dS )a  
    Return inverse multi-dimensional discrete Fourier transform.

    The sequence can be of an arbitrary type.

    The returned array contains::

      y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
         x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i)

    where ``d = len(x.shape)``, ``n = x.shape``, and ``p = prod[i=1..d] n_i``.

    For description of parameters see `fftn`.

    See Also
    --------
    fftn : for detailed information.

    Examples
    --------
    >>> from scipy.fftpack import fftn, ifftn
    >>> import numpy as np
    >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
    >>> np.allclose(y, ifftn(fftn(y)))
    True

    r@   )rU   )r'   r4   rR   rA   r   r   r   r     s    r@   c             C   s   t | |||S )z
    2-D discrete Fourier transform.

    Return the two-dimensional discrete Fourier transform of the 2-D argument
    `x`.

    See Also
    --------
    fftn : for detailed information.

    )r   )r'   r4   rR   rA   r   r   r   r     s    c             C   s   t | |||S )z
    2-D discrete inverse Fourier transform of real or complex sequence.

    Return inverse two-dimensional discrete Fourier transform of
    arbitrary type sequence x.

    See `ifft` for more information.

    See also
    --------
    fft2, ifft

    )r   )r'   r4   rR   rA   r   r   r   r     s    )Nr@   F)Nr@   F)Nr@   F)Nr@   F)NNF)NNF)NrW   F)NrW   F)5__doc__Z
__future__r   r   r   __all__r   r   r    r   Zscipy.fftpack.helperr   atexitregisterZdestroy_zfft_cacheZdestroy_zfftnd_cacheZdestroy_drfft_cacheZdestroy_cfft_cacheZdestroy_cfftnd_cacheZdestroy_rfft_cacher   r   r#   r*   r,   r/   r5   r   r.   Zfloat64r$   r&   rJ   r+   rH   r-   rN   r3   rV   r7   r?   rG   r   r   r	   r
   rT   r   rU   r   r   r   r   r   r   r   <module>   sZ   	
i
S
I
P'
=

