diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 2e9373405b5..28023153896 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -1033,31 +1033,6 @@ def prod( return dpnp.prod(self, axis, dtype, out, keepdims, initial, where) - def ptp( - self, - axis=None, - out=None, - keepdims=numpy._NoValue, - device=None, - usm_type=None, - sycl_queue=None, - ): - """ - Range of values (maximum - minimum) along an axis. - - For full documentation refer to :obj:`numpy.ptp`. - """ - - return dpnp.ptp( - self, - axis=axis, - out=out, - keepdims=keepdims, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, - ) - def put(self, indices, vals, /, *, axis=None, mode="wrap"): """ Puts values of an array into another array along a given axis. diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index fe88dea32b5..d1824bbf8f4 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -80,7 +80,6 @@ "ogrid", "ones", "ones_like", - "ptp", "trace", "tri", "tril", @@ -1602,69 +1601,6 @@ def ones_like( return call_origin(numpy.ones_like, x1, dtype, order, subok, shape) -def ptp( - arr, - /, - axis=None, - out=None, - keepdims=numpy._NoValue, - *, - device=None, - usm_type=None, - sycl_queue=None, -): - """ - Range of values (maximum - minimum) along an axis. - - For full documentation refer to :obj:`numpy.ptp`. - - Returns - ------- - ptp : dpnp.ndarray - The range of a given array. - - Limitations - ----------- - Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`. - Parameters `out` and `keepdims` are supported only with default values. - Otherwise the function will be executed sequentially on CPU. - - Examples - -------- - >>> import dpnp as np - >>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]]) - >>> np.ptp(x, axis=1) - array([8, 6]) - - >>> np.ptp(x, axis=0) - array([2, 0, 5, 2]) - - >>> np.ptp(x) - array(10) - """ - if not isinstance(arr, (dpnp.ndarray, dpt.usm_ndarray)): - pass - elif axis is not None and not isinstance(axis, int): - pass - elif out is not None: - pass - elif keepdims is not numpy._NoValue: - pass - else: - max_array = dpnp.max(arr, axis=axis) - min_array = dpnp.min(arr, axis=axis) - - _usm_type = arr.usm_type if usm_type is None else usm_type - _sycl_queue = dpnp.get_normalized_queue_device( - arr, sycl_queue=sycl_queue, device=device - ) - return dpnp.array( - max_array - min_array, usm_type=_usm_type, sycl_queue=_sycl_queue - ) - - return call_origin(numpy.ptp, arr, axis, out, keepdims) - - def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None): """ Return the sum along diagonals of the array. diff --git a/dpnp/dpnp_iface_statistics.py b/dpnp/dpnp_iface_statistics.py index 2374d25a9f2..5fbee3aee1e 100644 --- a/dpnp/dpnp_iface_statistics.py +++ b/dpnp/dpnp_iface_statistics.py @@ -60,6 +60,7 @@ "mean", "median", "min", + "ptp", "nanvar", "std", "var", @@ -621,6 +622,55 @@ def min(a, axis=None, out=None, keepdims=False, initial=None, where=True): return call_origin(numpy.min, a, axis, out, keepdims, initial, where) +def ptp( + a, + /, + axis=None, + out=None, + keepdims=False, +): + """ + Range of values (maximum - minimum) along an axis. + + For full documentation refer to :obj:`numpy.ptp`. + + Returns + ------- + ptp : dpnp.ndarray + The range of a given array. + + Limitations + ----------- + Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`. + Otherwise the function will be executed sequentially on CPU. + + Examples + -------- + >>> import dpnp as np + >>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]]) + >>> np.ptp(x, axis=1) + array([8, 6]) + + >>> np.ptp(x, axis=0) + array([2, 0, 5, 2]) + + >>> np.ptp(x) + array(10) + + """ + + if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)): + pass + else: + return dpnp.subtract( + dpnp.max(a, axis=axis, keepdims=keepdims), + dpnp.min(a, axis=axis, keepdims=keepdims), + out=out, + ) + + return call_origin(numpy.ptp, a, axis, out, keepdims) + + def nanvar(x1, axis=None, dtype=None, out=None, ddof=0, keepdims=False): """ Compute the variance along the specified axis, while ignoring NaNs. diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index 68ba7893394..da9153a3ed3 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -244,9 +244,7 @@ tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatte tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_copied tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_transposed -tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan -tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_imag -tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_real + tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_max tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_min diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 64abca3e05c..3ec70604aed 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -168,13 +168,11 @@ def test_array_creation_follow_device(func, args, kwargs, device): assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) -@pytest.mark.skip("muted until the issue reported by SAT-5969 is resolved") @pytest.mark.parametrize( "func, args, kwargs", [ pytest.param("diag", ["x0"], {}), pytest.param("diagflat", ["x0"], {}), - pytest.param("ptp", ["x0"], {"axis": 0}), ], ) @pytest.mark.parametrize( @@ -195,6 +193,7 @@ def test_array_creation_follow_device_2d_array(func, args, kwargs, device): assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) +@pytest.mark.skip("muted until the issue reported by SAT-5969 is resolved") @pytest.mark.parametrize( "func, args, kwargs", [ @@ -245,7 +244,6 @@ def test_array_creation_cross_device(func, args, kwargs, device_x, device_y): [ pytest.param("diag", ["x0"], {}), pytest.param("diagflat", ["x0"], {}), - pytest.param("ptp", ["x0"], {"axis": 0}), ], ) @pytest.mark.parametrize( @@ -343,6 +341,7 @@ def test_meshgrid(device_x, device_y): pytest.param("negative", [1.0, 0.0, -1.0]), pytest.param("positive", [1.0, 0.0, -1.0]), pytest.param("prod", [1.0, 2.0]), + pytest.param("ptp", [1.0, 2.0, 4.0, 7.0]), pytest.param( "real", [complex(1.0, 2.0), complex(3.0, 4.0), complex(5.0, 6.0)] ), diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index f15dc655b59..069b0cd2a2d 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -169,7 +169,6 @@ def test_array_creation_from_1d_array(func, args, usm_type_x, usm_type_y): [ pytest.param("diag", ["x0"]), pytest.param("diagflat", ["x0"]), - pytest.param("ptp", ["x0", "0"]), ], ) @pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types) @@ -376,6 +375,7 @@ def test_meshgrid(usm_type_x, usm_type_y): pytest.param("negative", [1.0, 0.0, -1.0]), pytest.param("positive", [1.0, 0.0, -1.0]), pytest.param("proj", [complex(1.0, 2.0), complex(dp.inf, -1.0)]), + pytest.param("ptp", [1.0, 2.0, 4.0, 7.0]), pytest.param( "real", [complex(1.0, 2.0), complex(3.0, 4.0), complex(5.0, 6.0)] ), diff --git a/tests/third_party/cupy/core_tests/test_ndarray_reduction.py b/tests/third_party/cupy/core_tests/test_ndarray_reduction.py index ceea3c6259c..69886bfdf49 100644 --- a/tests/third_party/cupy/core_tests/test_ndarray_reduction.py +++ b/tests/third_party/cupy/core_tests/test_ndarray_reduction.py @@ -9,7 +9,6 @@ from tests.third_party.cupy import testing -@pytest.mark.usefixtures("allow_fall_back_on_numpy") @testing.gpu class TestArrayReduction(unittest.TestCase): @testing.for_all_dtypes() @@ -149,70 +148,70 @@ def test_min_nan_imag(self, xp, dtype): @testing.numpy_cupy_allclose() def test_ptp_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) - return a.ptp() + return xp.ptp(a) @testing.with_requires("numpy>=1.15") @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_all_keepdims(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) - return a.ptp(keepdims=True) + return xp.ptp(a, keepdims=True) @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) - return a.ptp(axis=0) + return xp.ptp(a, axis=0) @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_axis0(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) - return a.ptp(axis=0) + return xp.ptp(a, axis=0) @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_axis1(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) - return a.ptp(axis=1) + return xp.ptp(a, axis=1) @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_axis2(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) - return a.ptp(axis=2) + return xp.ptp(a, axis=2) @testing.with_requires("numpy>=1.15") @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_multiple_axes(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) - return a.ptp(axis=(1, 2)) + return xp.ptp(a, axis=(1, 2)) @testing.with_requires("numpy>=1.15") @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_allclose() def test_ptp_multiple_axes_keepdims(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) - return a.ptp(axis=(1, 2), keepdims=True) + return xp.ptp(a, axis=(1, 2), keepdims=True) @testing.for_float_dtypes() @testing.numpy_cupy_allclose() def test_ptp_nan(self, xp, dtype): a = xp.array([float("nan"), 1, -1], dtype) - return a.ptp() + return xp.ptp(a) @testing.for_complex_dtypes() @testing.numpy_cupy_allclose() def test_ptp_nan_real(self, xp, dtype): a = xp.array([float("nan"), 1, -1], dtype) - return a.ptp() + return xp.ptp(a) @testing.for_complex_dtypes() @testing.numpy_cupy_allclose() def test_ptp_nan_imag(self, xp, dtype): a = xp.array([float("nan") * 1.0j, 1.0j, -1.0j], dtype) - return a.ptp() + return xp.ptp(a) # This class compares CUB results against NumPy's