o
    Nrf                     @  s  d dl mZ d dlZd dlZd dlmZ d dlZz
d dlZd dl	ZW n e
y-   dZY nw d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d	Zd
Zdd Zdd Zdd Zdd Zdd ZeeeeeedZd'ddZeej j Z eej j!Z!eej j"Z"eej j#Z#eej j$Z$eej j%Z%eej j&Z&eej j'Z'eej j(Z(eej j)Z)eej j*Z*eej j+Z+eej j,Z,eej j-Z-dd Z.eej d(ddZ/eej d(ddZ0d)d!d"Z1eej d*d#d$Z2eej d*d%d&Z3dS )+    )annotationsN)Sequence)asarray)concatenate)arange)NUMPY_GE_200)derived_fromskip_doctestzDask array only supports taking an FFT along an axis that 
has a single chunk. An FFT operation was tried on axis %s 
which has chunks %s. To change the array's chunks use dask.Array.rechunk.z
    Wrapping of %s

    The axis along which the FFT is applied must have only one chunk. To change
    the array's chunking use dask.Array.rechunk.

    The %s docstring follows below:

    c                 C  s<   |du r| j S t| j }t|D ]\}}|| f||< q|S )z*For computing the output chunks of [i]fft*N)chunkslist	enumerateasaxesr
   iaxis r   W/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dask/array/fft.py_fft_out_chunks'   s   
r   c                   sd   |du r fdd|D }t |}|d d d |d< t  j}t|D ]\}}|| f||< q$|S )z(For computing the output chunks of rfft*Nc                      g | ]	} j | d  qS r   r
   .0r   r   r   r   
<listcomp>4       z$_rfft_out_chunks.<locals>.<listcomp>      r   r
   r   r   r   r   r   _rfft_out_chunks1   s   
r"   c                   s\   |du r fdd|D }d|d d  |d< t  j}t|D ]\}}|| f||< q |S )z)For computing the output chunks of irfft*Nc                   r   r   r   r   r   r   r   r   @   r   z%_irfft_out_chunks.<locals>.<listcomp>r   r   r    r!   r   r   r   r   _irfft_out_chunks=   s   
r#   c                 C  sX   t |dksJ |d }|d u rd| j| d d  g}|d }t| j}|f||< |S Nr    r   r   lenr
   r   )r   r   r   r   nr
   r   r   r   _hfft_out_chunksH   s   

r(   c                 C  s   t |dksJ |d }|d u r| j| d g}nt |dks!J |d }t| j}|d dkr7|d d }n|d d }|f||< |S r$   r%   )r   r   r   r   r'   r
   mr   r   r   _ihfft_out_chunksW   s   

r*   )fftifftrfftirffthfftihfftc                   s   t durt jju rtdt jju rtddu rjz	td W n ty5   td w dfdd	}	drN| d fd	d	}t
}j}|jd
 | }jdurxtd|f  |_| jj7  _t|j|_||_|S )a|  Wrap 1D, 2D, and ND real and complex FFT functions

    Takes a function that behaves like ``numpy.fft`` functions and
    a specified kind to match it to that are named after the functions
    in the ``numpy.fft`` API.

    Supported kinds include:

        * fft
        * fft2
        * fftn
        * ifft
        * ifft2
        * ifftn
        * rfft
        * rfft2
        * rfftn
        * irfft
        * irfft2
        * irfftn
        * hfft
        * ihfft

    Examples
    --------
    >>> import dask.array.fft as dff
    >>> parallel_fft = dff.fft_wrap(np.fft.fft)
    >>> parallel_ifft = dff.fft_wrap(np.fft.ifft)
    Nz+SciPy's `rfft` doesn't match the NumPy API.z,SciPy's `irfft` doesn't match the NumPy API.Z2nzGiven unknown `kind` %s.c           
        sr  t | } |d u r5drd}n3dr2|d u r!tt| j}n"tr)tdt ttt	|}nd}nt	t
|t	|k rCtd }|d u rltj| jd | jd}z	||d	j}W n tyk   |j}Y nw |D ]}t	| j| d
krtt|| j| f qn| ||}||f}dr|d u rd n|d }|d u rd n|d }	|	|f}| jg|R ||dS )N2)r   r'   z^DeprecationWarning: `axes` should not be `None` if `s` is not `None` (Deprecated in NumPy 2.0))r   zDuplicate axes not allowed.)   )dtype)r   r    r+   r   r4   r
   )r   endswithtuplerangendimr   warningswarnDeprecationWarningr&   set
ValueErrornpZonesr4   	TypeErrorr
   chunk_error
map_blocks)
r   r   r   Z_dtypesampleZ	each_axisr
   argsr   r'   )r4   fft_funckindout_chunk_fnr   r   func   sF   


zfft_wrap.<locals>.funcr+   c                   s0   d }|d ur	|f}d }|d ur|f} | ||S Nr   )r   r'   r   r   r   )_funcr   r   rH      s   .r   NN)scipyZfftpackr-   r>   r.   __name___out_chunk_fnsrstripKeyErrorr6   inspect	getmodule__doc__fft_preambler	   )rE   rF   r4   rH   Zfunc_mod	func_nameZfunc_fullnamer   )rJ   r4   rE   rF   rG   r   fft_wrapv   s2   
-

rW   c                 C  s4   |   }|| |d d k  |8  < |||  }|S )Nr    r   )copy)r   r'   drr   r   r   _fftfreq_block   s   r[         ?autoc                 C  s0   t | } t|}t| t|d}|jtt| |dS )Nr5   )r4   r'   rY   )intfloat_arangerB   r[   r'   rY   r
   rZ   r   r   r   fftfreq   s   rb   c                 C  s6   t | } t|}t| d d t|d}|| |  }|S )Nr   r    r5   )r^   r_   r`   ra   r   r   r   rfftfreq  s
   rc   Fc           	      C  s   |d u rt t| j}nt|ts|f}| }|D ]V}|j| }|t|du  d }|jtd g }td |||< t|}|jtd g }t|d ||< t|}t	|| || g|d}t
| j| dkrn||| j| i}q|S )NFr   )r   r    )r   r8   r9   
isinstancer   shaper^   slicer7   _concatenater&   r
   Zrechunk)	xr   inverseyr   r'   Zn_2lrZ   r   r   r   _fftshift_helper  s&   

rl   c                 C     t | |ddS )NFr   ri   rl   rh   r   r   r   r   fftshift.     rq   c                 C  rm   )NTrn   ro   rp   r   r   r   	ifftshift3  rr   rs   rL   )r\   r]   )NFrI   )4
__future__r   rR   r:   collections.abcr   numpyr?   rM   Zscipy.fftpackImportErrorZdask.array.corer   r   rg   Zdask.array.creationr   r`   Zdask.array.numpy_compatr   Z
dask.utilsr   r	   rA   rU   r   r"   r#   r(   r*   rO   rW   r+   Zfft2Zfftnr,   Zifft2Zifftnr-   Zrfft2Zrfftnr.   Zirfft2Zirfftnr/   r0   r[   rb   rc   rl   rq   rs   r   r   r   r   <module>   sp    


q	

