From 93f6e954cc4ac72e0178db70a29b2963d9ba398a Mon Sep 17 00:00:00 2001 From: Nassim Berrada <112006029+nassimberrada@users.noreply.github.com> Date: Fri, 30 Dec 2022 09:48:58 +0100 Subject: [PATCH] numpy_copysign (#9238) Co-authored-by: nassimberrada --- .../mathematical_functions/miscellaneous.py | 22 +++++++ .../test_miscellaneous.py | 57 +++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/ivy/functional/frontends/numpy/mathematical_functions/miscellaneous.py b/ivy/functional/frontends/numpy/mathematical_functions/miscellaneous.py index 6063f77f6b513..d2260f255a04b 100644 --- a/ivy/functional/frontends/numpy/mathematical_functions/miscellaneous.py +++ b/ivy/functional/frontends/numpy/mathematical_functions/miscellaneous.py @@ -286,3 +286,25 @@ def interp_inner(value): return ivy.astype(ivy.array(ret[0]), "float64") else: return ivy.astype(ivy.array(ret), "float64") + + +@handle_numpy_dtype +@to_ivy_arrays_and_back +@handle_numpy_casting +@from_zero_dim_arrays_to_scalar +def copysign( + x1, + x2, + /, + out=None, + *, + where=True, + casting="same_kind", + order="k", + dtype=None, + subok=True, +): + ret = ivy.copysign(x1, x2, out=out) + if ivy.is_array(where): + ret = ivy.where(where, ret, ivy.default(out, ivy.zeros_like(ret)), out=out) + return ret diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py index ba40b269d4376..2a3d3ddbf5436 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py @@ -603,3 +603,60 @@ def test_numpy_convolve( v=xs[1], mode=mode, ) + + +# copysign +@handle_frontend_test( + fn_tree="numpy.copysign", + dtypes_values_casting=np_frontend_helpers.dtypes_values_casting_dtype( + arr_func=[ + lambda: helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=2, + shared_dtype=True, + min_value=-100, + max_value=100, + ) + ], + get_dtypes_kind="float", + ), + where=np_frontend_helpers.where(), +) +def test_numpy_copysign( + dtypes_values_casting, + where, + as_variable, + with_out, + num_positional_args, + native_array, + frontend, + fn_tree, + on_device, +): + input_dtype, xs, casting, dtype = dtypes_values_casting + where, as_variable, native_array = np_frontend_helpers.handle_where_and_array_bools( + where=where, + input_dtype=input_dtype, + as_variable=as_variable, + native_array=native_array, + ) + np_frontend_helpers.test_frontend_function( + input_dtypes=input_dtype, + as_variable_flags=as_variable, + with_out=with_out, + num_positional_args=num_positional_args, + native_array_flags=native_array, + frontend=frontend, + fn_tree=fn_tree, + on_device=on_device, + rtol=1e-2, + atol=1e-2, + x1=xs[0], + x2=xs[1], + out=None, + where=where, + casting=casting, + order="K", + dtype=dtype, + subok=True, + )