From 9d1bf8f2590fe4fb672b4b2ddcce4a2fadf22a24 Mon Sep 17 00:00:00 2001 From: Joboa Date: Fri, 10 Feb 2023 18:42:51 -0500 Subject: [PATCH] Add real function to Numpy frontend --- ivy/functional/frontends/numpy/__init__.py | 2 ++ .../handling_complex_numbers.py | 5 ++++ .../frontends/numpy/ufunc/methods.py | 3 ++- .../test_handling_complex_numbers.py | 24 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ivy/functional/frontends/numpy/__init__.py b/ivy/functional/frontends/numpy/__init__.py index 9d8809fa1f92e..f13381e27b81b 100644 --- a/ivy/functional/frontends/numpy/__init__.py +++ b/ivy/functional/frontends/numpy/__init__.py @@ -540,6 +540,7 @@ def promote_types_of_numpy_inputs( from ivy.functional.frontends.numpy.mathematical_functions.handling_complex_numbers import ( _imag, + _real, ) from ivy.functional.frontends.numpy.mathematical_functions.hyperbolic_functions import ( @@ -663,3 +664,4 @@ def promote_types_of_numpy_inputs( matmul = ufunc("_matmul") maximum = ufunc("_maximum") minimum = ufunc("_minimum") +real = ufunc("_real") diff --git a/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py b/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py index a88a694d2e8f9..c356d5b6f6d46 100644 --- a/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py +++ b/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py @@ -8,3 +8,8 @@ @to_ivy_arrays_and_back def _imag(val): return ivy.imag(val) + + +@to_ivy_arrays_and_back +def _real(val): + return ivy.real(val) diff --git a/ivy/functional/frontends/numpy/ufunc/methods.py b/ivy/functional/frontends/numpy/ufunc/methods.py index b8ac633c4ea98..d6df90269f082 100644 --- a/ivy/functional/frontends/numpy/ufunc/methods.py +++ b/ivy/functional/frontends/numpy/ufunc/methods.py @@ -50,7 +50,7 @@ "greater_equal", "heaviside", "hypot", - "imag" + "imag", "invert", "isfinite", "isinf", @@ -84,6 +84,7 @@ "power", "rad2deg", "radians", + "real", "reciprocal", "remainder", "right_shift", diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_handling_complex_numbers.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_handling_complex_numbers.py index 6436611bfa676..0a18023f3bbd9 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_handling_complex_numbers.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_handling_complex_numbers.py @@ -28,3 +28,27 @@ def test_numpy_imag( on_device=on_device, val=x[0], ) + + +# real +@handle_frontend_test( + fn_tree="numpy.real", + dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("valid")), + test_with_out=st.just(False), +) +def test_numpy_real( + dtype_and_x, + frontend, + test_flags, + fn_tree, + on_device, +): + input_dtypes, x = dtype_and_x + helpers.test_frontend_function( + input_dtypes=input_dtypes, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + val=x[0], + )