@@ -366,45 +366,62 @@ def fftshift(x, axes=None):
366
366
"""
367
367
Shift the zero-frequency component to the center of the spectrum.
368
368
369
+ This function swaps half-spaces for all axes listed (defaults to all).
370
+ Note that ``out[0]`` is the Nyquist component only if ``len(x)`` is even.
371
+
369
372
For full documentation refer to :obj:`numpy.fft.fftshift`.
370
373
371
- Limitations
372
- -----------
373
- Parameter `x` is supported either as :class:` dpnp.ndarray`.
374
- Parameter `axes` is unsupported .
375
- Only `dpnp.float64`, `dpnp.float32`, `dpnp.int64`, `dpnp.int32`,
376
- `dpnp.complex128` data types are supported .
377
- Otherwise the function will be executed sequentially on CPU .
374
+ Parameters
375
+ ----------
376
+ x : { dpnp.ndarray, usm_ndarray}
377
+ Input array .
378
+ axes : {None, int, list or tuple of ints}, optional
379
+ Axes over which to shift .
380
+ Default is ``None``, which shifts all axes .
378
381
379
- """
382
+ Returns
383
+ -------
384
+ out : dpnp.ndarray
385
+ The shifted array.
380
386
381
- x_desc = dpnp .get_dpnp_descriptor (x , copy_when_nondefault_queue = False )
382
- # TODO: enable implementation
383
- # pylint: disable=condition-evals-to-constant
384
- if x_desc and 0 :
385
- norm_ = Norm .backward
387
+ See Also
388
+ --------
389
+ :obj:`dpnp.fft.ifftshift` : The inverse of :obj:`dpnp.fft.fftshift`.
386
390
387
- if axes is None :
388
- axis_param = - 1 # the most right dimension (default value)
389
- else :
390
- axis_param = axes
391
+ Examples
392
+ --------
393
+ >>> import dpnp as np
394
+ >>> freqs = np.fft.fftfreq(10, 0.1)
395
+ >>> freqs
396
+ array([ 0., 1., 2., 3., 4., -5., -4., -3., -2., -1.])
397
+ >>> np.fft.fftshift(freqs)
398
+ array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])
399
+
400
+ Shift the zero-frequency component only along the second axis:
401
+
402
+ >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)
403
+ >>> freqs
404
+ array([[ 0., 1., 2.],
405
+ [ 3., 4., -4.],
406
+ [-3., -2., -1.]])
407
+ >>> np.fft.fftshift(freqs, axes=(1,))
408
+ array([[ 2., 0., 1.],
409
+ [-4., 3., 4.],
410
+ [-1., -3., -2.]])
391
411
392
- if x_desc .size < 1 :
393
- pass # let fallback to handle exception
394
- else :
395
- input_boundarie = x_desc .shape [axis_param ]
396
- output_boundarie = input_boundarie
412
+ """
397
413
398
- return dpnp_fft_deprecated (
399
- x_desc ,
400
- input_boundarie ,
401
- output_boundarie ,
402
- axis_param ,
403
- False ,
404
- norm_ .value ,
405
- ).get_pyobj ()
414
+ dpnp .check_supported_arrays_type (x )
415
+ if axes is None :
416
+ axes = tuple (range (x .ndim ))
417
+ shift = [dim // 2 for dim in x .shape ]
418
+ elif isinstance (axes , int ):
419
+ shift = x .shape [axes ] // 2
420
+ else :
421
+ x_shape = x .shape
422
+ shift = [x_shape [ax ] // 2 for ax in axes ]
406
423
407
- return call_origin ( numpy . fft . fftshift , x , axes )
424
+ return dpnp . roll ( x , shift , axes )
408
425
409
426
410
427
def hfft (x , n = None , axis = - 1 , norm = None ):
@@ -620,48 +637,55 @@ def ifftshift(x, axes=None):
620
637
"""
621
638
Inverse shift the zero-frequency component to the center of the spectrum.
622
639
623
- For full documentation refer to :obj:`numpy.fft.ifftshift`.
640
+ Although identical for even-length `x`, the functions differ by one sample
641
+ for odd-length `x`.
624
642
625
- Limitations
626
- -----------
627
- Parameter `x` is supported either as :class:`dpnp.ndarray`.
628
- Parameter `axes` is unsupported.
629
- Only `dpnp.float64`, `dpnp.float32`, `dpnp.int64`, `dpnp.int32`,
630
- `dpnp.complex128` data types are supported.
631
- Otherwise the function will be executed sequentially on CPU.
632
-
633
- """
643
+ For full documentation refer to :obj:`numpy.fft.ifftshift`.
634
644
635
- x_desc = dpnp .get_dpnp_descriptor (x , copy_when_nondefault_queue = False )
636
- # TODO: enable implementation
637
- # pylint: disable=condition-evals-to-constant
638
- if x_desc and 0 :
639
- norm_ = Norm .backward
645
+ Parameters
646
+ ----------
647
+ x : {dpnp.ndarray, usm_ndarray}
648
+ Input array.
649
+ axes : {None, int, list or tuple of ints}, optional
650
+ Axes over which to calculate.
651
+ Defaults to ``None``, which shifts all axes.
640
652
641
- if axes is None :
642
- axis_param = - 1 # the most right dimension (default value)
643
- else :
644
- axis_param = axes
653
+ Returns
654
+ -------
655
+ out : dpnp.ndarray
656
+ The shifted array.
645
657
646
- input_boundarie = x_desc .shape [axis_param ]
658
+ See Also
659
+ --------
660
+ :obj:`dpnp.fft.fftshift` : Shift zero-frequency component to the center
661
+ of the spectrum.
647
662
648
- if x_desc .size < 1 :
649
- pass # let fallback to handle exception
650
- elif input_boundarie < 1 :
651
- pass # let fallback to handle exception
652
- else :
653
- output_boundarie = input_boundarie
663
+ Examples
664
+ --------
665
+ >>> import dpnp as np
666
+ >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)
667
+ >>> freqs
668
+ array([[ 0., 1., 2.],
669
+ [ 3., 4., -4.],
670
+ [-3., -2., -1.]])
671
+ >>> np.fft.ifftshift(np.fft.fftshift(freqs))
672
+ array([[ 0., 1., 2.],
673
+ [ 3., 4., -4.],
674
+ [-3., -2., -1.]])
654
675
655
- return dpnp_fft_deprecated (
656
- x_desc ,
657
- input_boundarie ,
658
- output_boundarie ,
659
- axis_param ,
660
- True ,
661
- norm_ .value ,
662
- ).get_pyobj ()
676
+ """
663
677
664
- return call_origin (numpy .fft .ifftshift , x , axes )
678
+ dpnp .check_supported_arrays_type (x )
679
+ if axes is None :
680
+ axes = tuple (range (x .ndim ))
681
+ shift = [- (dim // 2 ) for dim in x .shape ]
682
+ elif isinstance (axes , int ):
683
+ shift = - (x .shape [axes ] // 2 )
684
+ else :
685
+ x_shape = x .shape
686
+ shift = [- (x_shape [ax ] // 2 ) for ax in axes ]
687
+
688
+ return dpnp .roll (x , shift , axes )
665
689
666
690
667
691
def ihfft (x , n = None , axis = - 1 , norm = None ):
0 commit comments