|
10 | 10 | _BOOLEAN_FFT_LENGTH_ERROR = "n must be an integer length, not boolean" |
11 | 11 | _FFT_SHAPE_SEQUENCE_ERROR = "s must be None or a sequence of integer lengths" |
12 | 12 | _FFT_AXES_SEQUENCE_ERROR = "axes must be None or a sequence of integer axes" |
| 13 | +_SHIFT_AXES_ERROR = "axes must be None, an integer axis, or a sequence of integer axes" |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def _normalize_real_fft_axis(axis): |
@@ -138,6 +139,50 @@ def _normalize_complex_fft_axes(axes): |
138 | 139 | ) |
139 | 140 |
|
140 | 141 |
|
| 142 | +def _normalize_shift_axes(axes): |
| 143 | + """Normalize scalar or sequence axis inputs accepted by FFT shifts.""" |
| 144 | + if axes is None: |
| 145 | + return None |
| 146 | + if isinstance(axes, (bool, _np.bool_)): |
| 147 | + raise TypeError(_BOOLEAN_FFT_AXIS_ERROR) |
| 148 | + if isinstance(axes, _np.ndarray): |
| 149 | + if _np.issubdtype(axes.dtype, _np.bool_): |
| 150 | + raise TypeError(_BOOLEAN_FFT_AXIS_ERROR) |
| 151 | + if axes.ndim == 0: |
| 152 | + return _normalize_fft_sequence_item( |
| 153 | + axes, _BOOLEAN_FFT_AXIS_ERROR, _SHIFT_AXES_ERROR |
| 154 | + ) |
| 155 | + return tuple( |
| 156 | + _normalize_fft_sequence_item( |
| 157 | + item, _BOOLEAN_FFT_AXIS_ERROR, _SHIFT_AXES_ERROR |
| 158 | + ) |
| 159 | + for item in axes.tolist() |
| 160 | + ) |
| 161 | + if isinstance(axes, _jnp.ndarray): |
| 162 | + axes_array = _np.asarray(axes) |
| 163 | + if _np.issubdtype(axes_array.dtype, _np.bool_): |
| 164 | + raise TypeError(_BOOLEAN_FFT_AXIS_ERROR) |
| 165 | + if axes_array.ndim == 0: |
| 166 | + return _normalize_fft_sequence_item( |
| 167 | + axes, _BOOLEAN_FFT_AXIS_ERROR, _SHIFT_AXES_ERROR |
| 168 | + ) |
| 169 | + return tuple( |
| 170 | + _normalize_fft_sequence_item( |
| 171 | + item, _BOOLEAN_FFT_AXIS_ERROR, _SHIFT_AXES_ERROR |
| 172 | + ) |
| 173 | + for item in axes_array.tolist() |
| 174 | + ) |
| 175 | + try: |
| 176 | + return _operator_index(axes) |
| 177 | + except TypeError: |
| 178 | + return _normalize_fft_integer_sequence( |
| 179 | + axes, |
| 180 | + _SHIFT_AXES_ERROR, |
| 181 | + _BOOLEAN_FFT_AXIS_ERROR, |
| 182 | + _SHIFT_AXES_ERROR, |
| 183 | + ) |
| 184 | + |
| 185 | + |
141 | 186 | def rfft(a, n=None, axis=-1, norm=None): |
142 | 187 | return _fft.rfft( |
143 | 188 | _jnp.asarray(a), |
@@ -175,8 +220,8 @@ def ifftn(a, s=None, axes=None, norm=None): |
175 | 220 |
|
176 | 221 |
|
177 | 222 | def fftshift(x, axes=None): |
178 | | - return _fft.fftshift(_jnp.asarray(x), axes=axes) |
| 223 | + return _fft.fftshift(_jnp.asarray(x), axes=_normalize_shift_axes(axes)) |
179 | 224 |
|
180 | 225 |
|
181 | 226 | def ifftshift(x, axes=None): |
182 | | - return _fft.ifftshift(_jnp.asarray(x), axes=axes) |
| 227 | + return _fft.ifftshift(_jnp.asarray(x), axes=_normalize_shift_axes(axes)) |
0 commit comments