Skip to content

Commit

Permalink
feat(frontend API): add as_complex and its test (ivy-llc#26020)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjsingh101 authored Sep 28, 2023
1 parent 5716f10 commit 1336e0e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ivy/functional/frontends/paddle/tensor/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,20 @@ def inner(self, y, name=None):
def mean(self, axis=None, keepdim=False, name=None):
return paddle_frontend.mean(self, axis=axis, keepdim=keepdim)

@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
def as_complex(self, name=None):
if self.ivy_array.shape[-1] != 2:
raise ivy.exceptions.IvyError(
"The size of the last dimension of tensor does not equals 2"
)
dtype = (
ivy.complex64 if ivy.dtype(self.ivy_array) == "float32" else ivy.complex128
)
re_part = self.ivy_array[..., 0]
im_part = ivy.multiply(1j, self.ivy_array[..., 1])
value = paddle_frontend.Tensor(ivy.add(re_part, im_part).astype(dtype))
return value

@with_supported_dtypes(
{"2.5.1 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ def _filter_query(query):
)


# as_complex
@st.composite
def _get_as_complex_inputs_(draw):
shape = draw(
helpers.get_shape(
min_num_dims=2, max_num_dims=5, min_dim_size=2, max_dim_size=10
)
)

x_dtype, x = draw(
helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"),
shape=(*shape, 2),
min_value=0,
max_value=50,
)
)
return x_dtype, x


# clip
@st.composite
def _get_clip_inputs(draw):
Expand Down Expand Up @@ -762,6 +782,37 @@ def test_paddle_tensor_argsort(
)


# as_complex
@handle_frontend_method(
class_tree=CLASS_TREE,
init_tree="paddle.to_tensor",
method_name="as_complex",
dtypes_and_x=_get_as_complex_inputs_(),
)
def test_paddle_tensor_as_complex(
dtypes_and_x,
frontend_method_data,
init_flags,
method_flags,
frontend,
on_device,
backend_fw,
):
input_dtype, x = dtypes_and_x
helpers.test_frontend_method(
init_input_dtypes=input_dtype,
backend_to_test=backend_fw,
init_all_as_kwargs_np={"data": x[0]},
method_input_dtypes=input_dtype,
method_all_as_kwargs_np={},
frontend_method_data=frontend_method_data,
init_flags=init_flags,
method_flags=method_flags,
frontend=frontend,
on_device=on_device,
)


# as_real
@handle_frontend_method(
class_tree=CLASS_TREE,
Expand Down

0 comments on commit 1336e0e

Please sign in to comment.