From 7959aa756d4316281086711245915ee01960e087 Mon Sep 17 00:00:00 2001 From: PushpamKJha <145205579+PushpamKJha@users.noreply.github.com> Date: Sat, 30 Sep 2023 01:04:34 +0530 Subject: [PATCH] feat: Add __ilshift__ numpy frontend (#24959) Co-authored-by: ZoeCD <42352932+ZoeCD@users.noreply.github.com> --- .../frontends/numpy/ndarray/ndarray.py | 3 ++ .../test_numpy/test_ndarray/test_ndarray.py | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/ivy/functional/frontends/numpy/ndarray/ndarray.py b/ivy/functional/frontends/numpy/ndarray/ndarray.py index 8ac0ccb0458c5..667c8ffd6c7ef 100644 --- a/ivy/functional/frontends/numpy/ndarray/ndarray.py +++ b/ivy/functional/frontends/numpy/ndarray/ndarray.py @@ -723,3 +723,6 @@ def _unsigned_int_bytes_repr(item_val, /, *, dtype=None): return b"".join(bytes_reprs) else: raise ValueError("Unsupported data type for the array.") + + def __ilshift__(self, value, /): + return ivy.bitwise_left_shift(self.ivy_array, value, out=self) diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py index 11e761c60e2ec..15dedfc733afc 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py @@ -3681,3 +3681,54 @@ def test_numpy_ndarray_view( frontend_method_data=frontend_method_data, on_device=on_device, ) + + +#__ilshift__ +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="numpy.array", + method_name="__ilshift__", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("integer"), + num_arrays=2, + max_dim_size=1, + max_value=2**31 - 1, + ), +) +def test_numpy_instance_ilshift__( + dtype_and_x, + frontend_method_data, + init_flags, + method_flags, + frontend, + backend_fw, + on_device, +): + input_dtypes, x = dtype_and_x + max_bits = np.iinfo(input_dtypes[0]).bits + max_shift = max_bits - 1 + x[1] = np.asarray(np.clip(x[1], 0, max_shift), dtype=input_dtypes[1]) + max_value_before_shift = 2 ** (max_bits - x[1]) - 1 + overflow_threshold = 2 ** (max_bits - 1) + x[0] = np.asarray( + np.clip(x[0], None, max_value_before_shift), dtype=input_dtypes[0] + ) + if np.any(x[0] > overflow_threshold): + x[0] = np.clip(x[0], None, overflow_threshold) + if np.any(x[0] < 0): + x[0] = np.abs(x[0]) + helpers.test_frontend_method( + init_input_dtypes=input_dtypes, + init_all_as_kwargs_np={ + "object": x[0], + }, + backend_to_test=backend_fw, + method_all_as_kwargs_np={ + "value": x[1], + }, + frontend=frontend, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + on_device=on_device, + )