
    CdT                       d dl mZ d dlZd dlmZ d dlZ	 d dlZd dlZn# e	$ r dZY nw xY w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
 Zd Zd Zd Zd ZeeeeeedZ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 Z+ eej                  dd            Z, eej                  dd            Z-ddZ. eej                  dd            Z/ eej                  dd            Z0dS )    )annotationsN)Sequence)asarray)concatenate)arange)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                    || j         S t          | j                   }t          |          D ]\  }}||         f||<   |S )z*For computing the output chunks of [i]fft*)chunkslist	enumerateasaxesr   iaxiss         .lib/python3.11/site-packages/dask/array/fft.py_fft_out_chunksr   %   sJ    yx!(^^FT??  4!wtM    c                     | fd|D             }t          |          }|d         dz  dz   |d<   t           j                  }t          |          D ]\  }}||         f||<   |S )z(For computing the output chunks of rfft*Nc                6    g | ]}j         |         d          S r   r   .0r   r   s     r   
<listcomp>z$_rfft_out_chunks.<locals>.<listcomp>2   $    0004QXd^A000r         r   r   r   r   s   `     r   _rfft_out_chunksr#   /   s}    y00004000QAbEQJNAbE!(^^FT??  4!wtMr   c                     | fd|D             }d|d         dz
  z  |d<   t           j                  }t          |          D ]\  }}||         f||<   |S )z)For computing the output chunks of irfft*Nc                6    g | ]}j         |         d          S r   r   r   s     r   r   z%_irfft_out_chunks.<locals>.<listcomp>>   r   r   r    r   r!   r"   r   s   `     r   _irfft_out_chunksr&   ;   sr    y00004000QrUQY"!(^^FT??  4!wtMr   c                    t          |          dk    sJ |d         }|d| j        |         d         dz
  z  g}|d         }t          | j                  }|f||<   |S Nr!   r   r    lenr   r   )r   r   r   r   nr   s         r   _hfft_out_chunksr,   F   sf    t99>>>>7Dy!(4.#a'()	!A!(^^F4F4LMr   c                   t          |          dk    sJ |d         }|| j        |         d         g}nt          |          dk    sJ |d         }t          | j                  }|dz  dk    r	|dz  dz   }n|dz   dz  }|f||<   |S r(   r)   )r   r   r   r   r+   r   ms          r   _ihfft_out_chunksr/   U   s    t99>>>>7DyXd^A1vv{{{{	!A!(^^F1uzz!VqLUqL4F4LMr   )fftifftrfftirffthfftihfftc                v    t           D t           j        j        u rt          d           t           j        j        u rt          d           j        	 t                              d                   n # t          $ r t          dz            w xY wd fd	}	                    d          r|dfd	}t          j                   } j        }|j        d	z   |z   } j        At          d
|fz  z  |_        |xj         j        z  c_        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.2nzGiven unknown `kind` %s.c                   t          |           } |~                    d          rd}n                    d          rN|"t          t          | j                            }nit          t          t          |                              }n?d}n<t          t          |                    t          |          k     rt          d          
}|Wt          j	        | j        dz  | j
                  }	  ||          j
        }n # t          $ r  |          j
        }Y nw xY w|D ]D}t          | j        |                   d	k    r$t          t          || j        |         fz            E | ||          }||f}                    d
          r|d n|d         }|d n|d         }	|	|f} | j        g|R ||dS )N2)r   r+   )r   zDuplicate axes not allowed.)   )dtype)r   r!   r0   r   r<   r   )r   endswithtuplerangendimr*   set
ValueErrornponesr<   	TypeErrorr   chunk_error
map_blocks)r   r   r   _dtypesample	each_axisr   argsr   r+   r<   fft_funckindout_chunk_fns             r   funczfft_wrap.<locals>.func   s   AJJ<}}S!! s## 9 qv//DD s1vv//DD3t99~~D		)) !>???>WQVd]!':::F0!&t444: 0 0 0!&))/0  	Q 	QI18I&''1,, 	18I;N/O!OPPP - aD))4y== 	<44T!WD	qtAt9Dq|HItII6&IIIIs   5D D%$D%r0   c                :    d }||f}d }||f} | ||          S N )r   r+   r   r   r   _funcs        r   rP   zfft_wrap.<locals>.func   s:    A}DDw5At$$$r   .r    NN)scipyfftpackr2   rC   r3   __name___out_chunk_fnsrstripKeyErrorr>   inspect	getmodule__doc__fft_preambler	   )	rM   rN   r<   rP   func_mod	func_namefunc_fullnamerT   rO   s	   ```    @@r   fft_wraprd   t   s   < u})))JKKK,,,KLLL| <%dkk$&7&78 < < <3d:;;;<$J $J $J $J $J $J $J $J $JL }}U %		% 		% 		% 		% 		% 		%  **H!I%+i7M##qM+;';<((#DL11DMKs    A< <Bc                r    |                                  }|| |dz   dz  k    xx         |z  cc<   |||z  z  }|S )Nr!   r    )copy)r   r+   drs       r   _fftfreq_blockri      sJ    	AaAEa<AQJAHr         ?autoc                    t          |           } t          |          }t          | t          |          }|                    t          t          | |          S )Nr=   )r<   r+   rg   )intfloat_arangerH   ri   r+   rg   r   rh   s       r   fftfreqrq      sF    AAaAv...A<<eqA<>>>r   c                    t          |           } t          |          }t          | dz  dz   t          |          }|| |z  z  }|S )Nr    r!   r=   )rm   rn   ro   rp   s       r   rfftfreqrs      sF    AAaAQ
%777AQJAHr   Fc                z   |"t          t          | j                            }nt          |t                    s|f}| }|D ]}|j        |         }|t          |du           z   dz  }|j        t          d           gz  }t          d |          ||<   t          |          }|j        t          d           gz  }t          |d           ||<   t          |          }t          ||         ||         g|          }t          | j        |                   dk    r"|                    || j        |         i          }|S )NFr    )r   r!   )r   r@   rA   
isinstancer   shaperm   slicer?   _concatenater*   r   rechunk)	xr   inverseyr   r+   n_2lrh   s	            r   _fftshift_helperr   
  s/   |E!&MM""h'' w	A , ,GAJ3w%'(((Q.FeDkk]"T3!!HHFeDkk]"S$!!HH!A$!A...qx{q  		1ahqk*++AHr   c                &    t          | |d          S )NFr   r{   r   rz   r   s     r   fftshiftr   %  s    AD%8888r   c                &    t          | |d          S )NTr   r   r   s     r   	ifftshiftr   *  s    AD$7777r   rV   )rj   rk   )NFrR   )1
__future__r   r]   collections.abcr   numpyrD   rW   scipy.fftpackImportErrordask.array.corer   r   rx   dask.array.creationr   ro   
dask.utilsr   r	   rG   r`   r   r#   r&   r,   r/   rZ   rd   r0   fft2fftnr1   ifft2ifftnr2   rfft2rfftnr3   irfft2irfftnr4   r5   ri   rq   rs   r   r   r   rS   r   r   <module>r      sQ   " " " " " "  $ $ $ $ $ $    LLL   EEE $ # # # # # 7 7 7 7 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1   	 	 	      ,  g g g gT hrvzxxxx	"&-	 	 	"&-	 	 x   bf? ? ? ? bf      6 bf9 9 9 9 bf8 8 8 8 8 8s    ))