From 2d70ddeda4e31b933cf09da14a7402501088f773 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 16 Mar 2023 15:39:27 +0000 Subject: [PATCH 1/2] Add angle function to NumPy Frontend --- .../handling_complex_numbers.py | 13 +++++++++ .../ivy/experimental/elementwise.py | 4 +-- .../test_handling_complex_numbers.py | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) 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 c356d5b6f6d46..fccfd761ec81a 100644 --- a/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py +++ b/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py @@ -5,6 +5,19 @@ ) +# TODO working for complex input only, could be expanded to handle whole +# range of input which numpy is capable of taking +@to_ivy_arrays_and_back +def angle(z, deg=False): + angle = ivy.angle(z, deg=deg) + + # numpy implementation multiplies by float64 when converting to degrees + # so implicit casting occures, but only to single value inputs (not arrays) + if deg and len(z.shape) == 0: + angle = ivy.astype(angle, ivy.float64) + return angle + + @to_ivy_arrays_and_back def _imag(val): return ivy.imag(val) diff --git a/ivy/functional/ivy/experimental/elementwise.py b/ivy/functional/ivy/experimental/elementwise.py index b6e95cefd41a8..e5cdb17d888c4 100644 --- a/ivy/functional/ivy/experimental/elementwise.py +++ b/ivy/functional/ivy/experimental/elementwise.py @@ -676,8 +676,8 @@ def angle( ------- ret Returns an array of angles for each complex number in the input. - If def is False(default), angle is calculated in radian and if - def is True, then angle is calculated in degrees. + If deg is False(default), angle is calculated in radian and if + deg is True, then angle is calculated in degrees. Examples -------- 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 0a18023f3bbd9..0afe5fc443418 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 @@ -6,6 +6,34 @@ from ivy_tests.test_ivy.helpers import handle_frontend_test +# angle +@handle_frontend_test( + fn_tree="numpy.angle", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("complex") + ), + deg=st.booleans(), +) +def test_numpy_angle( + dtype_and_x, + frontend, + test_flags, + fn_tree, + on_device, + deg, +): + 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, + z=x[0], + deg=deg, + ) + + # imag @handle_frontend_test( fn_tree="numpy.imag", From dea7cf7cbcf80d9cf2b3c922c43e51b79d95119c Mon Sep 17 00:00:00 2001 From: Muhammad Saeed Date: Fri, 17 Mar 2023 20:22:24 +0200 Subject: [PATCH 2/2] Update handling_complex_numbers.py --- .../numpy/mathematical_functions/handling_complex_numbers.py | 5 ----- 1 file changed, 5 deletions(-) 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 fccfd761ec81a..708be1e9ff291 100644 --- a/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py +++ b/ivy/functional/frontends/numpy/mathematical_functions/handling_complex_numbers.py @@ -5,14 +5,9 @@ ) -# TODO working for complex input only, could be expanded to handle whole -# range of input which numpy is capable of taking @to_ivy_arrays_and_back def angle(z, deg=False): angle = ivy.angle(z, deg=deg) - - # numpy implementation multiplies by float64 when converting to degrees - # so implicit casting occures, but only to single value inputs (not arrays) if deg and len(z.shape) == 0: angle = ivy.astype(angle, ivy.float64) return angle