Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented ifftshift and corresponding test #28707

Merged
merged 12 commits into from
Apr 9, 2024
Merged
27 changes: 27 additions & 0 deletions ivy/functional/frontends/jax/numpy/fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ def ifftn(a, s=None, axes=None, norm=None):
return a


@to_ivy_arrays_and_back
def ifftshift(x, axes=None):
# Check if an array
if not ivy.is_array(x):
raise ValueError("Input 'x' must be an array")

# Get the shape of x
shape = ivy.shape(x)

# If axes is None, shift all axes
if axes is None:
axes = tuple(range(x.ndim))

# Initialize a list to store the shift values
shift_values = []

# Calculate shift values for each axis
for axis in axes:
axis_size = shape[axis]
shift = -ivy.floor(axis_size / 2).astype(ivy.int32)
shift_values.append(shift)

# Perform the shift using Ivy's roll function
result = ivy.roll(x, shift_values, axes)
return result


@to_ivy_arrays_and_back
@with_unsupported_dtypes({"1.25.2 and below": ("float16", "bfloat16")}, "numpy")
def rfft(a, n=None, axis=-1, norm=None):
Expand Down
24 changes: 24 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,30 @@ def test_jax_numpy_ifftn(
)


# ifftshift
@handle_frontend_test(
fn_tree="jax.numpy.fft.ifftshift",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"), shape=(4,), array_api_dtypes=True
),
)
def test_jax_numpy_ifftshift(
dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device
):
input_dtype, arr = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
frontend=frontend,
backend_to_test=backend_fw,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
test_values=True,
x=arr[0],
axes=None,
)


# rfft
@handle_frontend_test(
fn_tree="jax.numpy.fft.rfft",
Expand Down
Loading