From f3f8b36d5bfb21907f321a4219ebb0fac7a88fe3 Mon Sep 17 00:00:00 2001 From: Pushpam Kumar Jha <135735962+PushpamJha14@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:41:29 +0530 Subject: [PATCH] feat: adding np.ndarray.var to the frontends (#22336) Co-authored-by: ivy-branch Co-authored-by: Anwaar Khalid --- .../frontends/numpy/ndarray/ndarray.py | 13 +++++ .../test_numpy/test_ndarray/test_ndarray.py | 49 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/ivy/functional/frontends/numpy/ndarray/ndarray.py b/ivy/functional/frontends/numpy/ndarray/ndarray.py index 667c8ffd6c7ef..4d199eb702ebf 100644 --- a/ivy/functional/frontends/numpy/ndarray/ndarray.py +++ b/ivy/functional/frontends/numpy/ndarray/ndarray.py @@ -621,6 +621,19 @@ def __lshift__(self, value, /): def round(self, decimals=0, out=None): return np_frontend.round(self, decimals=decimals, out=out) + def var( + self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True + ): + return np_frontend.var( + self, + axis=axis, + dtype=dtype, + out=out, + ddof=ddof, + keepdims=keepdims, + where=where, + ) + # --- Helpers --- # # --------------- # 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 b38a7239fa542..1b135b03020c0 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 @@ -11,6 +11,9 @@ assert_all_close, BackendHandler, ) +from ivy_tests.test_ivy.test_functional.test_core.test_statistical import ( + _statistical_dtype_values, +) import ivy_tests.test_ivy.test_frontends.test_numpy.helpers as np_frontend_helpers from ivy_tests.test_ivy.test_functional.test_core.test_linalg import ( _get_first_matrix_and_dtype, @@ -3699,6 +3702,52 @@ def test_numpy_ndarray_transpose( ) +# var +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="numpy.array", + method_name="var", + dtype_x_axis=_statistical_dtype_values(function="var"), + dtype=helpers.get_dtypes("valid", full=False, none=True), + where=np_frontend_helpers.where(), + keepdims=st.booleans(), +) +def test_numpy_ndarray_var( + dtype_x_axis, + frontend_method_data, + init_flags, + method_flags, + frontend, + backend_fw, + on_device, + keepdims, + where, + dtype, +): + input_dtypes, x, axis = dtype_x_axis + helpers.test_frontend_method( + init_input_dtypes=input_dtypes, + method_input_dtypes=input_dtypes, + init_all_as_kwargs_np={ + "object": x[0], + }, + method_all_as_kwargs_np={ + "axis": axis, + "dtype": dtype, + "keepdims": keepdims, + "where": where, + }, + frontend=frontend, + backend_to_test=backend_fw, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + rtol_=1e-2, + atol_=1e-2, + on_device=on_device, + ) + + # view @handle_frontend_method( class_tree=CLASS_TREE,