Skip to content

Commit

Permalink
Added ifft2 below Add Discrete Fourier Transform functions to Numpy F…
Browse files Browse the repository at this point in the history
…rontend #1532
  • Loading branch information
IsaiJimenezTalonia committed Sep 13, 2023
1 parent dfcbb15 commit 4b2a317
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/ivy.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/runConfigurations/_template__of_py_test.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def ifftshift(x, axes=None):
return roll


@with_unsupported_dtypes({"1.25.2 and below": ("float16",)}, "numpy")
@to_ivy_arrays_and_back
def iffttwo(a, shapeLength=None, axesNumber=2, norm=None):
a = ivy.asarray(a, dtype=ivy.complex128)
a = ivy.iffttwo(a, s=shapeLength, axes=axesNumber, norm=norm)
return a


@with_unsupported_dtypes({"1.25.2 and below": ("float16",)}, "numpy")
@to_ivy_arrays_and_back
def ihfft(a, n=None, axis=-1, norm=None):
Expand Down
86 changes: 86 additions & 0 deletions ivy/functional/ivy/experimental/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,92 @@ def fft2(
}


@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@handle_exceptions
@to_native_arrays_and_back
def iffttwo(
inputArray: Union[ivy.Array, ivy.NativeArray],
shapeLength: Optional[Union[int, Tuple[int, ...]]] = None,
axesNumber: Optional[Union[int, Tuple[int, ...]]] = None,
*,
normalization: str = "backward",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
r"""
Compute the 2-dimensional inverse discrete Fourier Transform.
Parameters
----------
inputArray:
Input array of complex numbers.
shapeLength:
Shape (length of transformed axis) of the output (`s[0]` refers to axis 0,
`s[1]` to axis 1, etc.). If given shape is smaller than that of the input,
the input is cropped. If larger, input is padded with zeros. If `s` is not
given, shape of input along axes specified by axes is used.
axesNumber:
Axes over which to compute the IFFT. If not given, last `len(s)` axes are
used, or all axes if `s` is also not specified. Repeated indices in axes
means inverse transform over that axis is performed multiple times.
normalization:
Indicates direction of the forward/backward pair of transforms is scaled
and with what normalization factor. "backward" indicates no normalization.
"ortho" indicates normalization by $\frac{1}{\sqrt{n}}$. "forward"
indicates normalization by $\frac{1}{n}$.
out
Optional output array for writing the result to. It must have a shape that
the inputs broadcast to.
Returns
-------
out
The truncated or zero-padded input, transformed along the axes indicated
by axes, or by a combination of s or x, as explained in the parameters
section above.
Raises
------
ValueError
If `s` and `axes` have different length.
IndexError
If an element of axes is larger than the number of axes of x.
Examples
--------
>>> inputArray = ivy.array([[0.24730653+0.90832391j, 0.49495562+0.9039565j,
... 0.98193269+0.49560517j],
... [0.93280757+0.48075343j, 0.28526384+0.3351205j,
... 0.2343787 +0.83528011j]])
>>> y = ivy.iffttwo(inputArray)
>>> print(y)
ivy.array([[ 0.51476765+0.66160417j, -0.04319742-0.05411636j,
-0.015561 -0.04216015j],
[ 0.06310689+0.05347854j, -0.13392983+0.16052352j,
-0.08371392+0.17252843j],
[-0.0031429 +0.05421245j, -0.10446617-0.17747098j,
0.05344324+0.07972424j]])
>>> inputArray = ivy.array([[0.24730653+0.90832391j, 0.49495562+0.9039565j,
... 0.98193269+0.49560517j],
... [0.93280757+0.48075343j, 0.28526384+0.3351205j,
... 0.2343787 +0.83528011j]])
>>>b=ivy.iffttwo(inputArray,shapeLength=[2,1],axesNumber=[0,1],normalization='ortho')
>>> print(b)
ivy.array([[ 0.8344667 +0.98222595j],
[-0.48472244+0.30233797j]])
"""
return ivy.current_backend(inputArray).iffttwo(
inputArray,
shapeLength=shapeLength,
axesNumber=axesNumber,
normalization=normalization,
out=out,
)


@handle_exceptions
@handle_backend_invalid
@handle_nestable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ivy_tests.test_ivy.test_functional.test_experimental.test_nn.test_layers import (
_x_and_ifft,
_x_and_rfftn,
_x_and_iffttwo,
)


Expand Down Expand Up @@ -153,6 +154,29 @@ def test_numpy_ifftshift(
)


@handle_frontend_test(
fn_tree="numpy.fft.iffttwo",
dtype_and_x=_x_and_iffttwo(),
)
def test_numpy_iffttwo(
dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device
):
input_dtype, x, dim, norm, n = 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,
a=x,
s=None,
axes=None,
norm=norm,
)


@handle_frontend_test(
fn_tree="numpy.fft.ihfft",
dtype_input_axis=helpers.dtype_values_axis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,42 @@ def _x_and_ifftn(draw):
return dtype, x, s, axes, norm


@st.composite
def _x_and_iffttwo(draw):
min_fft_points = 2
dtype = draw(helpers.get_dtypes("complex"))
x_dim = draw(
helpers.get_shape(
min_dim_size=2, max_dim_size=100, min_num_dims=1, max_num_dims=4
)
)
x = draw(
helpers.array_values(
dtype=dtype[0],
shape=tuple(x_dim),
min_value=-1e-10,
max_value=1e10,
)
)
axes = draw(
st.lists(
st.integers(0, len(x_dim) - 1), min_size=1, max_size=len(x_dim), unique=True
)
)
norm = draw(st.sampled_from(["forward", "ortho", "backward"]))

# Shape for s can be larger, smaller or equal to the size of the input
# along the axes specified by axes.
# Here, we're generating a list of integers corresponding to each axis in axes.
s = draw(
st.lists(
st.integers(min_fft_points, 256), min_size=len(axes), max_size=len(axes)
)
)

return dtype, x, s, axes, norm


@st.composite
def _x_and_rfftn(draw):
min_rfftn_points = 2
Expand Down Expand Up @@ -1011,6 +1047,34 @@ def test_ifftn(
)


@handle_test(
fn_tree="functional.ivy.experimental.iffttwo",
d_x_d_s_n=_x_and_iffttwo(),
ground_truth_backend="numpy",
test_gradients=st.just(False),
)
def test_iffttwo(
*,
d_x_d_s_n,
test_flags,
backend_fw,
fn_name,
on_device,
):
dtype, x, s, axes, norm = d_x_d_s_n
helpers.test_function(
input_dtypes=dtype,
test_flags=test_flags,
backend_to_test=backend_fw,
on_device=on_device,
fn_name=fn_name,
x=x,
s=s,
axes=axes,
norm=norm,
)


@handle_test(
fn_tree="functional.ivy.experimental.interpolate",
dtype_x_mode=_interp_args(),
Expand Down

0 comments on commit 4b2a317

Please sign in to comment.