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(numpy_frontends): implement real_if_close function. #23677

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,17 @@ def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):

@to_ivy_arrays_and_back
def real_if_close(a, tol=100):
return ivy.array(a) # ivy doesn't yet support complex numbers
a = ivy.array(a, dtype=a.dtype)
dtype_ = a.dtype

if not ivy.is_complex_dtype(dtype_):
return a

if tol > 1:
f = ivy.finfo(dtype_)
tol = f.eps * tol

if ivy.all(ivy.abs(ivy.imag(a)) < tol):
a = ivy.real(a)

return a
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,17 @@ def test_numpy_nan_to_num(
# real_if_close
@handle_frontend_test(
fn_tree="numpy.real_if_close",
dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("float")),
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float_and_complex"),
min_dim_size=1,
min_num_dims=1,
).filter(lambda x: "bfloat16" not in x[0]),
tol=st.integers(min_value=1, max_value=1000),
test_with_out=st.just(False),
)
def test_numpy_real_if_close(
dtype_and_x,
tol,
frontend,
test_flags,
fn_tree,
Expand All @@ -605,6 +611,7 @@ def test_numpy_real_if_close(
fn_tree=fn_tree,
on_device=on_device,
a=x[0],
tol=tol,
)


Expand Down
Loading