diff --git a/dpnp/tests/test_mathematical.py b/dpnp/tests/test_mathematical.py index d53be2fb4665..4d0f3919e4e5 100644 --- a/dpnp/tests/test_mathematical.py +++ b/dpnp/tests/test_mathematical.py @@ -9,8 +9,6 @@ from dpctl.utils import ExecutionPlacementError from numpy.testing import ( assert_allclose, - assert_almost_equal, - assert_array_almost_equal, assert_array_equal, assert_equal, assert_raises, @@ -25,6 +23,7 @@ generate_random_numpy_array, get_abs_array, get_all_dtypes, + get_array, get_complex_dtypes, get_float_complex_dtypes, get_float_dtypes, @@ -44,26 +43,26 @@ @pytest.mark.parametrize("deg", [True, False]) class TestAngle: def test_angle_bool(self, deg): - dp_a = dpnp.array([True, False]) - np_a = dp_a.asnumpy() + ia = dpnp.array([True, False]) + a = ia.asnumpy() - expected = numpy.angle(np_a, deg=deg) - result = dpnp.angle(dp_a, deg=deg) + expected = numpy.angle(a, deg=deg) + result = dpnp.angle(ia, deg=deg) - # In NumPy, for boolean arguments the output data type is always default floating data type. - # while data type of output in DPNP is determined by Type Promotion Rules. - # data type should not be compared - assert_allclose(result.asnumpy(), expected) + # In NumPy, for boolean arguments the output data type is always + # default floating data type. while data type of output in DPNP is + # determined by Type Promotion Rules. + assert_dtype_allclose(result, expected, check_only_type_kind=True) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_angle(self, dtype, deg): - dp_a = dpnp.arange(10, dtype=dtype) - np_a = dp_a.asnumpy() + ia = dpnp.arange(10, dtype=dtype) + a = ia.asnumpy() - expected = numpy.angle(np_a, deg=deg) - result = dpnp.angle(dp_a, deg=deg) + expected = numpy.angle(a, deg=deg) + result = dpnp.angle(ia, deg=deg) # For dtype=int8, uint8, NumPy returns float16, but dpnp returns float32 dt_int8 = dtype in [dpnp.int8, dpnp.uint8] @@ -71,14 +70,11 @@ def test_angle(self, dtype, deg): @pytest.mark.parametrize("dtype", get_complex_dtypes()) def test_angle_complex(self, dtype, deg): - a = numpy.random.rand(10) - b = numpy.random.rand(10) - np_a = numpy.array(a + 1j * b, dtype=dtype) - dp_a = dpnp.array(np_a) - - expected = numpy.angle(np_a, deg=deg) - result = dpnp.angle(dp_a, deg=deg) + a = generate_random_numpy_array(10, dtype=dtype) + ia = dpnp.array(a) + expected = numpy.angle(a, deg=deg) + result = dpnp.angle(ia, deg=deg) assert_dtype_allclose(result, expected) @@ -98,15 +94,17 @@ def test_conj(self, dtype): assert result is ia assert_dtype_allclose(result, a.conj()) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_bool=True) + ) def test_conj_out(self, dtype): a = generate_random_numpy_array(20, dtype) ia = dpnp.array(a) expected = numpy.conj(a) - dp_out = dpnp.empty(ia.shape, dtype=dtype) - result = dpnp.conj(ia, out=dp_out) - assert dp_out is result + iout = dpnp.empty(ia.shape, dtype=dtype) + result = dpnp.conj(ia, out=iout) + assert iout is result assert_dtype_allclose(result, expected) @@ -115,7 +113,7 @@ class TestConvolve: def test_object(self): d = [1.0] * 100 k = [1.0] * 3 - assert_array_almost_equal(dpnp.convolve(d, k)[2:-2], dpnp.full(98, 3)) + assert_array_equal(dpnp.convolve(d, k)[2:-2], dpnp.full(98, 3.0)) def test_no_overwrite(self): d = dpnp.ones(100) @@ -127,7 +125,7 @@ def test_no_overwrite(self): def test_mode(self): d = dpnp.ones(100) k = dpnp.ones(3) - default_mode = dpnp.convolve(d, k, mode="full") + default_mode = dpnp.convolve(d, k) full_mode = dpnp.convolve(d, k, mode="full") assert_array_equal(full_mode, default_mode) # integer mode @@ -145,12 +143,12 @@ class TestClip: ) @pytest.mark.parametrize("order", ["C", "F", "A", "K", None]) def test_clip(self, dtype, order): - dp_a = dpnp.asarray([[1, 2, 8], [1, 6, 4], [9, 5, 1]], dtype=dtype) - np_a = dpnp.asnumpy(dp_a) + ia = dpnp.asarray([[1, 2, 8], [1, 6, 4], [9, 5, 1]], dtype=dtype) + a = dpnp.asnumpy(ia) - result = dpnp.clip(dp_a, 2, 6, order=order) - expected = numpy.clip(np_a, 2, 6, order=order) - assert_allclose(expected, result) + result = dpnp.clip(ia, 2, 6, order=order) + expected = numpy.clip(a, 2, 6, order=order) + assert_allclose(result, expected) assert expected.flags.c_contiguous == result.flags.c_contiguous assert expected.flags.f_contiguous == result.flags.f_contiguous @@ -158,15 +156,15 @@ def test_clip(self, dtype, order): "dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True) ) def test_clip_arrays(self, dtype): - dp_a = dpnp.asarray([1, 2, 8, 1, 6, 4, 1], dtype=dtype) - np_a = dpnp.asnumpy(dp_a) + ia = dpnp.asarray([1, 2, 8, 1, 6, 4, 1], dtype=dtype) + a = dpnp.asnumpy(ia) - min_v = dpnp.asarray(2, dtype=dtype) - max_v = dpnp.asarray(6, dtype=dtype) + min_v = dpnp.array(2, dtype=dtype) + max_v = dpnp.array(6, dtype=dtype) - result = dpnp.clip(dp_a, min_v, max_v) - expected = numpy.clip(np_a, min_v.asnumpy(), max_v.asnumpy()) - assert_allclose(expected, result) + result = dpnp.clip(ia, min_v, max_v) + expected = numpy.clip(a, min_v.asnumpy(), max_v.asnumpy()) + assert_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True) @@ -174,24 +172,24 @@ def test_clip_arrays(self, dtype): @pytest.mark.parametrize("in_dp", [dpnp, dpt]) @pytest.mark.parametrize("out_dp", [dpnp, dpt]) def test_clip_out(self, dtype, in_dp, out_dp): - np_a = numpy.array([[1, 2, 8], [1, 6, 4], [9, 5, 1]], dtype=dtype) - dp_a = in_dp.asarray(np_a) + a = numpy.array([[1, 2, 8], [1, 6, 4], [9, 5, 1]], dtype=dtype) + ia = in_dp.asarray(a) - dp_out = out_dp.ones(dp_a.shape, dtype=dtype) - np_out = numpy.ones(np_a.shape, dtype=dtype) + iout = out_dp.ones(ia.shape, dtype=dtype) + out = numpy.ones(a.shape, dtype=dtype) - result = dpnp.clip(dp_a, 2, 6, out=dp_out) - expected = numpy.clip(np_a, 2, 6, out=np_out) - assert_allclose(expected, result) - assert_allclose(np_out, dp_out) + result = dpnp.clip(ia, 2, 6, out=iout) + expected = numpy.clip(a, 2, 6, out=out) + assert_allclose(result, expected) + assert_allclose(iout, out) assert isinstance(result, dpnp_array) def test_input_nan(self): - np_a = numpy.array([-2.0, numpy.nan, 0.5, 3.0, 0.25, numpy.nan]) - dp_a = dpnp.array(np_a) + a = numpy.array([-2.0, numpy.nan, 0.5, 3.0, 0.25, numpy.nan]) + ia = dpnp.array(a) - result = dpnp.clip(dp_a, -1, 1) - expected = numpy.clip(np_a, -1, 1) + result = dpnp.clip(ia, -1, 1) + expected = numpy.clip(a, -1, 1) assert_array_equal(result, expected) @testing.with_requires("numpy>=1.25.0") @@ -206,12 +204,12 @@ def test_input_nan(self): ], ) def test_nan_edges(self, kwargs): - np_a = numpy.arange(7.0) - dp_a = dpnp.asarray(np_a) + a = numpy.arange(7.0) + ia = dpnp.asarray(a) - result = dp_a.clip(**kwargs) - expected = np_a.clip(**kwargs) - assert_allclose(expected, result) + result = ia.clip(**kwargs) + expected = a.clip(**kwargs) + assert_allclose(result, expected) @pytest.mark.parametrize( "kwargs", @@ -247,12 +245,14 @@ def _assert_arrays(self, res, exp, axis, include_initial): assert_dtype_allclose(res, exp) def _get_exp_array(self, a, axis, dtype): - np_a = dpnp.asnumpy(a) + a = dpnp.asnumpy(a) if axis != None: - return numpy.logaddexp.accumulate(np_a, axis=axis, dtype=dtype) - return numpy.logaddexp.accumulate(np_a.ravel(), dtype=dtype) + return numpy.logaddexp.accumulate(a, axis=axis, dtype=dtype) + return numpy.logaddexp.accumulate(a.ravel(), dtype=dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) @pytest.mark.parametrize("axis", [None, 2, -1]) @pytest.mark.parametrize("include_initial", [True, False]) def test_basic(self, dtype, axis, include_initial): @@ -267,10 +267,12 @@ def test_basic(self, dtype, axis, include_initial): exp = self._get_exp_array(a, axis, exp_dt) self._assert_arrays(res, exp, axis, include_initial) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) @pytest.mark.parametrize("axis", [None, 2, -1]) @pytest.mark.parametrize("include_initial", [True, False]) - def test_out(self, dtype, axis, include_initial): + def test_include_initial(self, dtype, axis, include_initial): a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype) if dpnp.issubdtype(a, dpnp.float32): @@ -305,41 +307,39 @@ def test_axis_tuple(self): assert_raises(TypeError, dpnp.cumlogsumexp, a, axis=(0, 1)) @pytest.mark.parametrize( - "in_dtype", get_all_dtypes(no_bool=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) - @pytest.mark.parametrize("out_dtype", get_all_dtypes(no_bool=True)) - def test_dtype(self, in_dtype, out_dtype): - a = dpnp.ones(100, dtype=in_dtype) - res = dpnp.cumlogsumexp(a, dtype=out_dtype) - exp = numpy.logaddexp.accumulate(dpnp.asnumpy(a)) - exp = exp.astype(out_dtype) + @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True)) + def test_dtype(self, in_dt, dt): + a = dpnp.ones(100, dtype=in_dt) + res = dpnp.cumlogsumexp(a, dtype=dt) + exp = numpy.logaddexp.accumulate(dpnp.asnumpy(a)).astype(res.dtype) dtype_list = [dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16] - tol = 1e-2 if in_dtype in dtype_list else 1e-6 + tol = 1e-2 if in_dt in dtype_list else 1e-6 assert_allclose(res, exp, rtol=tol) @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") @pytest.mark.parametrize( - "arr_dt", get_all_dtypes(no_none=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_complex=True) ) @pytest.mark.parametrize( "out_dt", get_all_dtypes(no_none=True, no_complex=True) ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(10, 20).reshape((2, 5)).astype(dtype=arr_dt) + def test_out(self, in_dt, out_dt): + a = numpy.arange(10, 20).reshape(2, 5).astype(dtype=in_dt) out = numpy.zeros_like(a, dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = dpnp.cumlogsumexp(ia, out=iout, dtype=dtype, axis=1) + result = dpnp.cumlogsumexp(ia, out=iout, axis=1) exp = numpy.logaddexp.accumulate(a, out=out, axis=1) - assert_allclose(result, exp.astype(dtype), rtol=1e-06) + assert result is iout + assert_allclose(result, exp) -class TestCumProd: +class TestCumProdCumSum: + @pytest.mark.parametrize("func", ["cumprod", "cumsum"]) @pytest.mark.parametrize( "arr, axis", [ @@ -348,24 +348,16 @@ class TestCumProd: pytest.param([[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]], -1), ], ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_axis(self, arr, axis, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + def test_axis(self, func, arr, axis, dtype): a = numpy.array(arr, dtype=dtype) ia = dpnp.array(a) - result = dpnp.cumprod(ia, axis=axis) - expected = numpy.cumprod(a, axis=axis) - assert_array_equal(expected, result) - - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_ndarray_method(self, dtype): - a = numpy.arange(1, 10).astype(dtype=dtype) - ia = dpnp.array(a) - - result = ia.cumprod() - expected = a.cumprod() - assert_array_equal(expected, result) + result = getattr(dpnp, func)(ia, axis=axis) + expected = getattr(numpy, func)(a, axis=axis) + assert_array_equal(result, expected) + @pytest.mark.parametrize("func", ["cumprod", "cumsum"]) @pytest.mark.parametrize("sh", [(10,), (2, 5)]) @pytest.mark.parametrize( "xp_in, xp_out, check", @@ -375,173 +367,81 @@ def test_ndarray_method(self, dtype): pytest.param(dpnp, dpt, False), ], ) - def test_usm_ndarray(self, sh, xp_in, xp_out, check): - a = numpy.arange(-12, -2).reshape(sh) + def test_usm_ndarray(self, func, sh, xp_in, xp_out, check): + a = generate_random_numpy_array(sh, low=-5, high=5) ia = xp_in.asarray(a) - result = dpnp.cumprod(ia) - expected = numpy.cumprod(a) - assert_array_equal(expected, result) + result = getattr(dpnp, func)(ia) + expected = getattr(numpy, func)(a) + assert_dtype_allclose(result, expected) - out = numpy.empty((10,)) + out = numpy.empty(10) iout = xp_out.asarray(out) - result = dpnp.cumprod(ia, out=iout) - expected = numpy.cumprod(a, out=out) - assert_array_equal(expected, result) + result = getattr(dpnp, func)(ia, out=iout) + expected = getattr(numpy, func)(a, out=out) + assert_dtype_allclose(result, expected) assert (result is iout) is check @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(1, 6).astype(dtype=arr_dt) - out = numpy.zeros_like(a, dtype=out_dt) - - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = ia.cumprod(out=iout, dtype=dtype) - expected = a.cumprod(out=out, dtype=dtype) - assert_array_equal(expected, result) - assert result is iout - - @testing.with_requires("numpy>=2.1.0") - def test_include_initial(self): - a = numpy.arange(8).reshape(2, 2, 2) - ia = dpnp.array(a) - - expected = numpy.cumulative_prod(a, axis=1, include_initial=True) - result = dpnp.cumulative_prod(ia, axis=1, include_initial=True) - assert_array_equal(result, expected) - - expected = numpy.cumulative_prod(a, axis=0, include_initial=True) - result = dpnp.cumulative_prod(ia, axis=0, include_initial=True) - assert_array_equal(result, expected) - - a = numpy.arange(1, 5).reshape(2, 2) - ia = dpnp.array(a) - out = numpy.zeros((3, 2), dtype=numpy.float32) - out_dp = dpnp.array(out) - - expected = numpy.cumulative_prod( - a, axis=0, out=out, include_initial=True - ) - result = dpnp.cumulative_prod( - ia, axis=0, out=out_dp, include_initial=True - ) - assert result is out_dp - assert_array_equal(result, expected) - - a = numpy.array([2, 2]) - ia = dpnp.array(a) - expected = numpy.cumulative_prod(a, include_initial=True) - result = dpnp.cumulative_prod(ia, include_initial=True) - assert_array_equal(result, expected) - - -class TestCumSum: - @pytest.mark.parametrize( - "arr, axis", - [ - pytest.param([1, 2, 10, 11, 6, 5, 4], 0), - pytest.param([[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]], 0), - pytest.param([[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]], 1), - pytest.param([[0, 1, 2], [3, 4, 5]], 0), - pytest.param([[0, 1, 2], [3, 4, 5]], -1), - ], - ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_axis(self, arr, axis, dtype): - a = numpy.array(arr, dtype=dtype) - ia = dpnp.array(a) - - result = dpnp.cumsum(ia, axis=axis) - expected = numpy.cumsum(a, axis=axis) - assert_array_equal(expected, result) - - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_ndarray_method(self, dtype): - a = numpy.arange(10).astype(dtype=dtype) + @pytest.mark.parametrize("func", ["cumprod", "cumsum"]) + @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("dt", get_all_dtypes()) + def test_dtype(self, func, in_dt, dt): + a = generate_random_numpy_array(5, dtype=in_dt, low=-5, high=5) ia = dpnp.array(a) - result = ia.cumsum() - expected = a.cumsum() - assert_array_equal(expected, result) + expected = getattr(a, func)(dtype=dt) + result = getattr(ia, func)(dtype=dt) + assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("sh", [(10,), (2, 5)]) + # TODO: include boolean dtype when dpctl-0.20.0 is being used in Internal CI + @pytest.mark.usefixtures("suppress_complex_warning") + @pytest.mark.parametrize("func", ["cumprod", "cumsum"]) + @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( - "xp_in, xp_out, check", - [ - pytest.param(dpt, dpt, False), - pytest.param(dpt, dpnp, True), - pytest.param(dpnp, dpt, False), - ], + "out_dt", get_all_dtypes(no_none=True, no_bool=True) ) - def test_usm_ndarray(self, sh, xp_in, xp_out, check): - a = numpy.arange(10).reshape(sh) - ia = xp_in.asarray(a) - - result = dpnp.cumsum(ia) - expected = numpy.cumsum(a) - assert_array_equal(expected, result) - - out = numpy.empty((10,)) - iout = xp_out.asarray(out) - - result = dpnp.cumsum(ia, out=iout) - expected = numpy.cumsum(a, out=out) - assert_array_equal(expected, result) - assert (result is iout) is check - - @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(5, 15).astype(dtype=arr_dt) + def test_out(self, func, in_dt, out_dt): + a = generate_random_numpy_array(5, dtype=in_dt, low=-5, high=5) out = numpy.zeros_like(a, dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = ia.cumsum(out=iout, dtype=dtype) - expected = a.cumsum(out=out, dtype=dtype) - assert_array_equal(expected, result) + expected = getattr(a, func)(out=out) + result = getattr(ia, func)(out=iout) assert result is iout + assert_allclose(result, expected, rtol=1e-06) @testing.with_requires("numpy>=2.1.0") - def test_include_initial(self): + @pytest.mark.parametrize("func", ["cumulative_prod", "cumulative_sum"]) + def test_include_initial(self, func): a = numpy.arange(8).reshape(2, 2, 2) ia = dpnp.array(a) - expected = numpy.cumulative_sum(a, axis=1, include_initial=True) - result = dpnp.cumulative_sum(ia, axis=1, include_initial=True) + expected = getattr(numpy, func)(a, axis=1, include_initial=True) + result = getattr(dpnp, func)(ia, axis=1, include_initial=True) assert_array_equal(result, expected) - expected = numpy.cumulative_sum(a, axis=0, include_initial=True) - result = dpnp.cumulative_sum(ia, axis=0, include_initial=True) + expected = getattr(numpy, func)(a, axis=0, include_initial=True) + result = getattr(dpnp, func)(ia, axis=0, include_initial=True) assert_array_equal(result, expected) a = numpy.arange(1, 5).reshape(2, 2) ia = dpnp.array(a) out = numpy.zeros((3, 2), dtype=numpy.float32) - out_dp = dpnp.array(out) + iout = dpnp.array(out) - expected = numpy.cumulative_sum( + expected = getattr(numpy, func)( a, axis=0, out=out, include_initial=True ) - result = dpnp.cumulative_sum( - ia, axis=0, out=out_dp, include_initial=True - ) - assert result is out_dp + result = getattr(dpnp, func)(ia, axis=0, out=iout, include_initial=True) + assert result is iout assert_array_equal(result, expected) a = numpy.array([2, 2]) ia = dpnp.array(a) - expected = numpy.cumulative_sum(a, include_initial=True) - result = dpnp.cumulative_sum(ia, include_initial=True) + expected = getattr(numpy, func)(a, include_initial=True) + result = getattr(dpnp, func)(ia, include_initial=True) assert_array_equal(result, expected) @@ -550,53 +450,53 @@ class TestDiff: @pytest.mark.parametrize("dt", get_integer_dtypes()) def test_basic_integer(self, n, dt): x = [1, 4, 6, 7, 12] - np_a = numpy.array(x, dtype=dt) - dpnp_a = dpnp.array(x, dtype=dt) + a = numpy.array(x, dtype=dt) + ia = dpnp.array(x, dtype=dt) - expected = numpy.diff(np_a, n=n) - result = dpnp.diff(dpnp_a, n=n) - assert_array_equal(expected, result) + expected = numpy.diff(a, n=n) + result = dpnp.diff(ia, n=n) + assert_array_equal(result, expected) @pytest.mark.parametrize("dt", get_float_dtypes()) def test_basic_floating(self, dt): x = [1.1, 2.2, 3.0, -0.2, -0.1] - np_a = numpy.array(x, dtype=dt) - dpnp_a = dpnp.array(x, dtype=dt) + a = numpy.array(x, dtype=dt) + ia = dpnp.array(x, dtype=dt) - expected = numpy.diff(np_a) - result = dpnp.diff(dpnp_a) - assert_almost_equal(expected, result) + expected = numpy.diff(a) + result = dpnp.diff(ia) + assert_array_equal(result, expected) @pytest.mark.parametrize("n", [1, 2]) def test_basic_boolean(self, n): x = [True, True, False, False] - np_a = numpy.array(x) - dpnp_a = dpnp.array(x) + a = numpy.array(x) + ia = dpnp.array(x) - expected = numpy.diff(np_a, n=n) - result = dpnp.diff(dpnp_a, n=n) - assert_array_equal(expected, result) + expected = numpy.diff(a, n=n) + result = dpnp.diff(ia, n=n) + assert_array_equal(result, expected) @pytest.mark.parametrize("dt", get_complex_dtypes()) def test_basic_complex(self, dt): x = [1.1 + 1j, 2.2 + 4j, 3.0 + 6j, -0.2 + 7j, -0.1 + 12j] - np_a = numpy.array(x, dtype=dt) - dpnp_a = dpnp.array(x, dtype=dt) + a = numpy.array(x, dtype=dt) + ia = dpnp.array(x, dtype=dt) - expected = numpy.diff(np_a) - result = dpnp.diff(dpnp_a) - assert_allclose(expected, result) + expected = numpy.diff(a) + result = dpnp.diff(ia) + assert_allclose(result, expected) @pytest.mark.parametrize("axis", [None] + list(range(-3, 2))) def test_axis(self, axis): - np_a = numpy.zeros((10, 20, 30)) - np_a[:, 1::2, :] = 1 - dpnp_a = dpnp.array(np_a) + a = numpy.zeros((10, 20, 30)) + a[:, 1::2, :] = 1 + ia = dpnp.array(a) kwargs = {} if axis is None else {"axis": axis} - expected = numpy.diff(np_a, **kwargs) - result = dpnp.diff(dpnp_a, **kwargs) - assert_array_equal(expected, result) + expected = numpy.diff(a, **kwargs) + result = dpnp.diff(ia, **kwargs) + assert_array_equal(result, expected) @pytest.mark.parametrize("xp", [numpy, dpnp]) @pytest.mark.parametrize("axis", [-4, 3]) @@ -612,25 +512,25 @@ def test_ndim_error(self, xp): @pytest.mark.parametrize("n", [None, 2]) @pytest.mark.parametrize("axis", [None, 0]) def test_nd(self, n, axis): - np_a = 20 * numpy.random.rand(10, 20, 30) - dpnp_a = dpnp.array(np_a) + a = 20 * numpy.random.rand(10, 20, 30) + ia = dpnp.array(a) kwargs = {} if n is None else {"n": n} if axis is not None: kwargs.update({"axis": axis}) - expected = numpy.diff(np_a, **kwargs) - result = dpnp.diff(dpnp_a, **kwargs) + expected = numpy.diff(a, **kwargs) + result = dpnp.diff(ia, **kwargs) assert_dtype_allclose(result, expected) @pytest.mark.parametrize("n", list(range(0, 5))) def test_n(self, n): - np_a = numpy.array(list(range(3))) - dpnp_a = dpnp.array(np_a) + a = numpy.array(list(range(3))) + ia = dpnp.array(a) - expected = numpy.diff(np_a, n=n) - result = dpnp.diff(dpnp_a, n=n) - assert_array_equal(expected, result) + expected = numpy.diff(a, n=n) + result = dpnp.diff(ia, n=n) + assert_array_equal(result, expected) @pytest.mark.parametrize("xp", [numpy, dpnp]) def test_n_error(self, xp): @@ -639,15 +539,15 @@ def test_n_error(self, xp): @pytest.mark.parametrize("prepend", [0, [0], [-1, 0]]) def test_prepend(self, prepend): - np_a = numpy.arange(5) + 1 - dpnp_a = dpnp.array(np_a) + a = numpy.arange(5) + 1 + ia = dpnp.array(a) np_p = prepend if numpy.isscalar(prepend) else numpy.array(prepend) dpnp_p = prepend if dpnp.isscalar(prepend) else dpnp.array(prepend) - expected = numpy.diff(np_a, prepend=np_p) - result = dpnp.diff(dpnp_a, prepend=dpnp_p) - assert_array_equal(expected, result) + expected = numpy.diff(a, prepend=np_p) + result = dpnp.diff(ia, prepend=dpnp_p) + assert_array_equal(result, expected) @pytest.mark.parametrize( "axis, prepend", @@ -659,27 +559,27 @@ def test_prepend(self, prepend): ], ) def test_prepend_axis(self, axis, prepend): - np_a = numpy.arange(4).reshape(2, 2) - dpnp_a = dpnp.array(np_a) + a = numpy.arange(4).reshape(2, 2) + ia = dpnp.array(a) np_p = prepend if numpy.isscalar(prepend) else numpy.array(prepend) dpnp_p = prepend if dpnp.isscalar(prepend) else dpnp.array(prepend) - expected = numpy.diff(np_a, axis=axis, prepend=np_p) - result = dpnp.diff(dpnp_a, axis=axis, prepend=dpnp_p) - assert_array_equal(expected, result) + expected = numpy.diff(a, axis=axis, prepend=np_p) + result = dpnp.diff(ia, axis=axis, prepend=dpnp_p) + assert_array_equal(result, expected) @pytest.mark.parametrize("append", [0, [0], [0, 2]]) def test_append(self, append): - np_a = numpy.arange(5) - dpnp_a = dpnp.array(np_a) + a = numpy.arange(5) + ia = dpnp.array(a) - np_ap = append if numpy.isscalar(append) else numpy.array(append) - dpnp_ap = append if dpnp.isscalar(append) else dpnp.array(append) + ap = append if numpy.isscalar(append) else numpy.array(append) + iap = append if dpnp.isscalar(append) else dpnp.array(append) - expected = numpy.diff(np_a, append=np_ap) - result = dpnp.diff(dpnp_a, append=dpnp_ap) - assert_array_equal(expected, result) + expected = numpy.diff(a, append=ap) + result = dpnp.diff(ia, append=iap) + assert_array_equal(result, expected) @pytest.mark.parametrize( "axis, append", @@ -691,15 +591,15 @@ def test_append(self, append): ], ) def test_append_axis(self, axis, append): - np_a = numpy.arange(4).reshape(2, 2) - dpnp_a = dpnp.array(np_a) + a = numpy.arange(4).reshape(2, 2) + ia = dpnp.array(a) - np_ap = append if numpy.isscalar(append) else numpy.array(append) - dpnp_ap = append if dpnp.isscalar(append) else dpnp.array(append) + ap = append if numpy.isscalar(append) else numpy.array(append) + iap = append if dpnp.isscalar(append) else dpnp.array(append) - expected = numpy.diff(np_a, axis=axis, append=np_ap) - result = dpnp.diff(dpnp_a, axis=axis, append=dpnp_ap) - assert_array_equal(expected, result) + expected = numpy.diff(a, axis=axis, append=ap) + result = dpnp.diff(ia, axis=axis, append=iap) + assert_array_equal(result, expected) @pytest.mark.parametrize("xp", [numpy, dpnp]) def test_prepend_append_error(self, xp): @@ -716,7 +616,9 @@ def test_prepend_append_axis_error(self, xp): class TestEdiff1d: - @pytest.mark.parametrize("data_type", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_bool=True) + ) @pytest.mark.parametrize( "array", [ @@ -726,13 +628,13 @@ class TestEdiff1d: [[1, 2, 3], [5, 2, 8], [7, 3, 4]], ], ) - def test_ediff1d(self, array, data_type): - np_a = numpy.array(array, dtype=data_type) - dpnp_a = dpnp.array(array, dtype=data_type) + def test_basic(self, array, dtype): + a = numpy.array(array, dtype=dtype) + ia = dpnp.array(a) - result = dpnp.ediff1d(dpnp_a) - expected = numpy.ediff1d(np_a) - assert_array_equal(expected, result) + result = dpnp.ediff1d(ia) + expected = numpy.ediff1d(a) + assert_array_equal(result, expected) @pytest.mark.parametrize( "to_begin", @@ -745,18 +647,14 @@ def test_ediff1d(self, array, data_type): (1, 2), ], ) - def test_ediff1d_to_begin(self, to_begin): - np_a = numpy.array([1, 2, 4, 7, 0]) - dpnp_a = dpnp.array([1, 2, 4, 7, 0]) - - if isinstance(to_begin, dpnp.ndarray): - np_to_begin = dpnp.asnumpy(to_begin) - else: - np_to_begin = to_begin + def test_to_begin(self, to_begin): + a = numpy.array([1, 2, 4, 7, 0]) + ia = dpnp.array(a) + np_to_begin = get_array(numpy, to_begin) - result = dpnp.ediff1d(dpnp_a, to_begin=to_begin) - expected = numpy.ediff1d(np_a, to_begin=np_to_begin) - assert_array_equal(expected, result) + result = dpnp.ediff1d(ia, to_begin=to_begin) + expected = numpy.ediff1d(a, to_begin=np_to_begin) + assert_array_equal(result, expected) @pytest.mark.parametrize( "to_end", @@ -769,18 +667,14 @@ def test_ediff1d_to_begin(self, to_begin): (3, 4), ], ) - def test_ediff1d_to_end(self, to_end): - np_a = numpy.array([1, 2, 4, 7, 0]) - dpnp_a = dpnp.array([1, 2, 4, 7, 0]) - - if isinstance(to_end, dpnp.ndarray): - np_to_end = dpnp.asnumpy(to_end) - else: - np_to_end = to_end + def test_to_end(self, to_end): + a = numpy.array([1, 2, 4, 7, 0]) + ia = dpnp.array(a) + np_to_end = get_array(numpy, to_end) - result = dpnp.ediff1d(dpnp_a, to_end=to_end) - expected = numpy.ediff1d(np_a, to_end=np_to_end) - assert_array_equal(expected, result) + result = dpnp.ediff1d(ia, to_end=to_end) + expected = numpy.ediff1d(a, to_end=np_to_end) + assert_array_equal(result, expected) @pytest.mark.parametrize( "to_begin, to_end", @@ -793,23 +687,16 @@ def test_ediff1d_to_end(self, to_end): ((1, 2), (3, 4)), ], ) - def test_ediff1d_to_begin_to_end(self, to_begin, to_end): - np_a = numpy.array([1, 2, 4, 7, 0]) - dpnp_a = dpnp.array([1, 2, 4, 7, 0]) + def test_to_begin_to_end(self, to_begin, to_end): + a = numpy.array([1, 2, 4, 7, 0]) + ia = dpnp.array(a) - if isinstance(to_begin, dpnp.ndarray): - np_to_begin = dpnp.asnumpy(to_begin) - else: - np_to_begin = to_begin + np_to_begin = get_array(numpy, to_begin) + np_to_end = get_array(numpy, to_end) - if isinstance(to_end, dpnp.ndarray): - np_to_end = dpnp.asnumpy(to_end) - else: - np_to_end = to_end - - result = dpnp.ediff1d(dpnp_a, to_end=to_end, to_begin=to_begin) - expected = numpy.ediff1d(np_a, to_end=np_to_end, to_begin=np_to_begin) - assert_array_equal(expected, result) + result = dpnp.ediff1d(ia, to_end=to_end, to_begin=to_begin) + expected = numpy.ediff1d(a, to_end=np_to_end, to_begin=np_to_begin) + assert_array_equal(result, expected) @pytest.mark.parametrize( "to_begin, to_end", @@ -821,9 +708,9 @@ def test_ediff1d_to_begin_to_end(self, to_begin, to_end): ((1, 2), (3, 4)), ], ) - def test_ediff1d_usm_ndarray(self, to_begin, to_end): - np_a = numpy.array([[1, 2, 0]]) - dpt_a = dpt.asarray(np_a) + def test_usm_ndarray(self, to_begin, to_end): + a = numpy.array([[1, 2, 0]]) + dpt_a = dpt.asarray(a) if isinstance(to_begin, dpt.usm_ndarray): np_to_begin = dpt.asnumpy(to_begin) @@ -836,37 +723,35 @@ def test_ediff1d_usm_ndarray(self, to_begin, to_end): np_to_end = to_end result = dpnp.ediff1d(dpt_a, to_end=to_end, to_begin=to_begin) - expected = numpy.ediff1d(np_a, to_end=np_to_end, to_begin=np_to_begin) + expected = numpy.ediff1d(a, to_end=np_to_end, to_begin=np_to_begin) - assert_array_equal(expected, result) + assert_array_equal(result, expected) assert isinstance(result, dpnp.ndarray) - def test_ediff1d_errors(self): - a_dp = dpnp.array([[1, 2], [2, 5]]) + def test_errors(self): + ia = dpnp.array([[1, 2], [2, 5]]) # unsupported type - a_np = dpnp.asnumpy(a_dp) - assert_raises(TypeError, dpnp.ediff1d, a_np) + a = dpnp.asnumpy(ia) + assert_raises(TypeError, dpnp.ediff1d, a) # unsupported `to_begin` type according to the `same_kind` rules to_begin = dpnp.array([-5], dtype="f4") - assert_raises(TypeError, dpnp.ediff1d, a_dp, to_begin=to_begin) + assert_raises(TypeError, dpnp.ediff1d, ia, to_begin=to_begin) # unsupported `to_end` type according to the `same_kind` rules to_end = dpnp.array([5], dtype="f4") - assert_raises(TypeError, dpnp.ediff1d, a_dp, to_end=to_end) + assert_raises(TypeError, dpnp.ediff1d, ia, to_end=to_end) # another `to_begin` sycl queue to_begin = dpnp.array([-20, -15], sycl_queue=dpctl.SyclQueue()) assert_raises( - ExecutionPlacementError, dpnp.ediff1d, a_dp, to_begin=to_begin + ExecutionPlacementError, dpnp.ediff1d, ia, to_begin=to_begin ) # another `to_end` sycl queue to_end = dpnp.array([15, 20], sycl_queue=dpctl.SyclQueue()) - assert_raises( - ExecutionPlacementError, dpnp.ediff1d, a_dp, to_end=to_end - ) + assert_raises(ExecutionPlacementError, dpnp.ediff1d, ia, to_end=to_end) class TestFix: @@ -1274,26 +1159,23 @@ def test_return_type(self): assert type(res) is tuple -@pytest.mark.parametrize("dtype1", get_all_dtypes()) -@pytest.mark.parametrize("dtype2", get_all_dtypes()) +@pytest.mark.parametrize("dtype1", get_all_dtypes(no_none=True)) +@pytest.mark.parametrize("dtype2", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "func", ["add", "divide", "multiply", "power", "subtract"] ) -@pytest.mark.parametrize("data", [[[1, 2], [3, 4]]], ids=["[[1, 2], [3, 4]]"]) -def test_op_multiple_dtypes(dtype1, func, dtype2, data): - np_a = numpy.array(data, dtype=dtype1) - dpnp_a = dpnp.array(data, dtype=dtype1) - - np_b = numpy.array(data, dtype=dtype2) - dpnp_b = dpnp.array(data, dtype=dtype2) +def test_op_multiple_dtypes(dtype1, func, dtype2): + a = numpy.array([[1, 2], [3, 4]], dtype=dtype1) + b = numpy.array([[1, 2], [3, 4]], dtype=dtype2) + ia, ib = dpnp.array(a), dpnp.array(b) if func == "subtract" and (dtype1 == dtype2 == dpnp.bool): with pytest.raises(TypeError): - result = getattr(dpnp, func)(dpnp_a, dpnp_b) - expected = getattr(numpy, func)(np_a, np_b) + result = getattr(dpnp, func)(ia, ib) + expected = getattr(numpy, func)(a, b) else: - result = getattr(dpnp, func)(dpnp_a, dpnp_b) - expected = getattr(numpy, func)(np_a, np_b) + result = getattr(dpnp, func)(ia, ib) + expected = getattr(numpy, func)(a, b) assert_allclose(result, expected) @@ -1380,11 +1262,11 @@ def array_or_scalar(xp, data, dtype=None): return xp.array(data, dtype=dtype) def _test_mathematical(self, name, dtype, lhs, rhs, check_type=True): - a_dpnp = self.array_or_scalar(dpnp, lhs, dtype=dtype) - b_dpnp = self.array_or_scalar(dpnp, rhs, dtype=dtype) + ia = self.array_or_scalar(dpnp, lhs, dtype=dtype) + ib = self.array_or_scalar(dpnp, rhs, dtype=dtype) - a_np = self.array_or_scalar(numpy, lhs, dtype=dtype) - b_np = self.array_or_scalar(numpy, rhs, dtype=dtype) + a = self.array_or_scalar(numpy, lhs, dtype=dtype) + b = self.array_or_scalar(numpy, rhs, dtype=dtype) if ( name == "subtract" @@ -1392,45 +1274,47 @@ def _test_mathematical(self, name, dtype, lhs, rhs, check_type=True): and dtype == dpnp.bool ): with pytest.raises(TypeError): - result = getattr(dpnp, name)(a_dpnp, b_dpnp) - expected = getattr(numpy, name)(a_np, b_np) + result = getattr(dpnp, name)(ia, ib) + expected = getattr(numpy, name)(a, b) else: - result = getattr(dpnp, name)(a_dpnp, b_dpnp) - expected = getattr(numpy, name)(a_np, b_np) + result = getattr(dpnp, name)(ia, ib) + expected = getattr(numpy, name)(a, b) assert_dtype_allclose(result, expected, check_type) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_add(self, dtype, lhs, rhs): self._test_mathematical("add", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) def test_arctan2(self, dtype, lhs, rhs): self._test_mathematical("arctan2", dtype, lhs, rhs) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_copysign(self, dtype, lhs, rhs): self._test_mathematical("copysign", dtype, lhs, rhs) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_divide(self, dtype, lhs, rhs): self._test_mathematical("divide", dtype, lhs, rhs) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_fmax(self, dtype, lhs, rhs): self._test_mathematical("fmax", dtype, lhs, rhs, check_type=False) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_fmin(self, dtype, lhs, rhs): self._test_mathematical("fmin", dtype, lhs, rhs, check_type=False) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_fmod(self, dtype, lhs, rhs): if rhs == 0.3 and not has_support_aspect64(): @@ -1446,7 +1330,9 @@ def test_fmod(self, dtype, lhs, rhs): pytest.skip("Due to accuracy reason, the results are different.") self._test_mathematical("fmod", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) def test_floor_divide(self, dtype, lhs, rhs): if dtype == dpnp.float32 and rhs == 0.3: pytest.skip( @@ -1457,27 +1343,30 @@ def test_floor_divide(self, dtype, lhs, rhs): ) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_hypot(self, dtype, lhs, rhs): self._test_mathematical("hypot", dtype, lhs, rhs) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_maximum(self, dtype, lhs, rhs): self._test_mathematical("maximum", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_minimum(self, dtype, lhs, rhs): self._test_mathematical("minimum", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_multiply(self, dtype, lhs, rhs): self._test_mathematical("multiply", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) def test_remainder(self, dtype, lhs, rhs): if ( - dtype in [dpnp.int32, dpnp.int64, None] + dpnp.issubdtype(dtype, dpnp.integer) + or dtype is None and rhs == 0.3 and not has_support_aspect64() ): @@ -1493,11 +1382,13 @@ def test_remainder(self, dtype, lhs, rhs): pytest.skip("Due to accuracy reason, the results are different.") self._test_mathematical("remainder", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_power(self, dtype, lhs, rhs): self._test_mathematical("power", dtype, lhs, rhs, check_type=False) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_bool=True) + ) def test_subtract(self, dtype, lhs, rhs): self._test_mathematical("subtract", dtype, lhs, rhs, check_type=False) @@ -1505,13 +1396,13 @@ def test_subtract(self, dtype, lhs, rhs): class TestNanToNum: @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("shape", [(3,), (2, 3), (3, 2, 2)]) - def test_nan_to_num(self, dtype, shape): - a = numpy.random.randn(*shape).astype(dtype) + def test_basic(self, dtype, shape): + a = generate_random_numpy_array(shape, dtype=dtype) if not dpnp.issubdtype(dtype, dpnp.integer): a.flat[1] = numpy.nan - a_dp = dpnp.array(a) + ia = dpnp.array(a) - result = dpnp.nan_to_num(a_dp) + result = dpnp.nan_to_num(ia) expected = numpy.nan_to_num(a) assert_allclose(result, expected) @@ -1539,8 +1430,8 @@ def test_errors(self): ia = dpnp.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf]) # unsupported type `a` - a_np = dpnp.asnumpy(ia) - assert_raises(TypeError, dpnp.nan_to_num, a_np) + a = dpnp.asnumpy(ia) + assert_raises(TypeError, dpnp.nan_to_num, a) # unsupported type `nan` i_nan = dpnp.array(1) @@ -1569,116 +1460,100 @@ def test_error_readonly(self): @pytest.mark.parametrize("copy", [True, False]) @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True, no_none=True)) - def test_nan_to_num_strided(self, copy, dt): + def test_strided(self, copy, dt): n = 10 dt = numpy.dtype(dt) - np_a = numpy.arange(2 * n, dtype=dt) - dp_a = dpnp.arange(2 * n, dtype=dt) + a = numpy.arange(2 * n, dtype=dt) + ia = dpnp.arange(2 * n, dtype=dt) if dt.kind in "fc": - np_a[::4] = numpy.nan - dp_a[::4] = dpnp.nan - dp_r = dpnp.nan_to_num(dp_a[::-2], copy=copy, nan=57.0) - np_r = numpy.nan_to_num(np_a[::-2], copy=copy, nan=57.0) + a[::4] = numpy.nan + ia[::4] = dpnp.nan + result = dpnp.nan_to_num(ia[::-2], copy=copy, nan=57.0) + expected = numpy.nan_to_num(a[::-2], copy=copy, nan=57.0) - assert_dtype_allclose(dp_r, np_r) + assert_dtype_allclose(result, expected) class TestProd: @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2, (1, 2), (0, -2)]) @pytest.mark.parametrize("keepdims", [False, True]) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) - def test_prod(self, axis, keepdims, dtype): - a = numpy.arange(1, 13, dtype=dtype).reshape((2, 2, 3)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + def test_basic(self, axis, keepdims, dtype): + a = generate_random_numpy_array((2, 2, 3), dtype=dtype, low=-5, high=5) ia = dpnp.array(a) - np_res = numpy.prod(a, axis=axis, keepdims=keepdims) - dpnp_res = dpnp.prod(ia, axis=axis, keepdims=keepdims) + expected = numpy.prod(a, axis=axis, keepdims=keepdims) + result = dpnp.prod(ia, axis=axis, keepdims=keepdims) - assert dpnp_res.shape == np_res.shape - assert_allclose(dpnp_res, np_res) + assert result.shape == expected.shape + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2, (1, 2), (0, -2)]) - def test_prod_zero_size(self, axis): + def test_zero_size(self, axis): a = numpy.empty((2, 3, 0)) ia = dpnp.array(a) - np_res = numpy.prod(a, axis=axis) - dpnp_res = dpnp.prod(ia, axis=axis) - assert_dtype_allclose(dpnp_res, np_res) - - @pytest.mark.parametrize("axis", [None, 0, 1, -1]) - @pytest.mark.parametrize("keepdims", [False, True]) - def test_prod_bool(self, axis, keepdims): - a = numpy.arange(2, dtype=numpy.bool_) - a = numpy.tile(a, (2, 2)) - ia = dpnp.array(a) - - np_res = numpy.prod(a, axis=axis, keepdims=keepdims) - dpnp_res = dpnp.prod(ia, axis=axis, keepdims=keepdims) - assert_dtype_allclose(dpnp_res, np_res) + expected = numpy.prod(a, axis=axis) + result = dpnp.prod(ia, axis=axis) + assert_dtype_allclose(result, expected) @pytest.mark.usefixtures("suppress_complex_warning") @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") - @pytest.mark.parametrize("in_dtype", get_all_dtypes(no_bool=True)) - @pytest.mark.parametrize( - "out_dtype", get_all_dtypes(no_bool=True, no_none=True) - ) - def test_prod_dtype(self, in_dtype, out_dtype): - a = numpy.arange(1, 13, dtype=in_dtype).reshape((2, 2, 3)) + @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("dt", get_all_dtypes(no_none=True)) + def test_dtype(self, in_dt, dt): + a = generate_random_numpy_array((2, 2, 3), dtype=in_dt, low=-5, high=5) ia = dpnp.array(a) - np_res = numpy.prod(a, dtype=out_dtype) - dpnp_res = dpnp.prod(ia, dtype=out_dtype) - assert_dtype_allclose(dpnp_res, np_res) + expected = numpy.prod(a, dtype=dt) + result = dpnp.prod(ia, dtype=dt) + assert_dtype_allclose(result, expected) @pytest.mark.usefixtures( "suppress_overflow_encountered_in_cast_numpy_warnings" ) - def test_prod_out(self): + def test_out(self): ia = dpnp.arange(1, 7).reshape((2, 3)) ia = ia.astype(dpnp.default_float_type(ia.device)) a = dpnp.asnumpy(ia) # output is dpnp_array - np_res = numpy.prod(a, axis=0) - dpnp_out = dpnp.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = dpnp.prod(ia, axis=0, out=dpnp_out) - assert dpnp_out is dpnp_res - assert_allclose(dpnp_res, np_res) + expected = numpy.prod(a, axis=0) + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.prod(ia, axis=0, out=iout) + assert iout is result + assert_allclose(result, expected) # output is usm_ndarray - dpt_out = dpt.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = dpnp.prod(ia, axis=0, out=dpt_out) - assert dpt_out is dpnp_res.get_array() - assert_allclose(dpnp_res, np_res) + dpt_out = dpt.empty(expected.shape, dtype=expected.dtype) + result = dpnp.prod(ia, axis=0, out=dpt_out) + assert dpt_out is result.get_array() + assert_allclose(result, expected) # out is a numpy array -> TypeError - dpnp_res = numpy.empty_like(np_res) + result = numpy.empty_like(expected) with pytest.raises(TypeError): - dpnp.prod(ia, axis=0, out=dpnp_res) + dpnp.prod(ia, axis=0, out=result) # incorrect shape for out - dpnp_res = dpnp.array(numpy.empty((2, 3))) + result = dpnp.array(numpy.empty((2, 3))) with pytest.raises(ValueError): - dpnp.prod(ia, axis=0, out=dpnp_res) + dpnp.prod(ia, axis=0, out=result) @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_prod_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(1, 7).reshape((2, 3)).astype(dtype=arr_dt) + def test_out_dtype(self, in_dt, out_dt): + a = generate_random_numpy_array((2, 3), dtype=in_dt, low=-5, high=5) out = numpy.zeros_like(a, shape=(2,), dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = dpnp.prod(ia, out=iout, dtype=dtype, axis=1) - expected = numpy.prod(a, out=out, dtype=dtype, axis=1) - assert_array_equal(expected, result) + result = dpnp.prod(ia, out=iout, axis=1) + expected = numpy.prod(a, out=out, axis=1) + assert_allclose(result, expected, rtol=1e-06) assert result is iout - def test_prod_Error(self): + def test_error(self): ia = dpnp.arange(5) with pytest.raises(TypeError): @@ -1692,7 +1567,7 @@ def test_prod_Error(self): class TestRealIfClose: @pytest.mark.parametrize("dt", get_all_dtypes(no_none=True)) def test_basic(self, dt): - a = numpy.random.rand(10).astype(dt) + a = generate_random_numpy_array(10, dtype=dt) ia = dpnp.array(a) result = dpnp.real_if_close(ia + 1e-15j) @@ -1701,7 +1576,7 @@ def test_basic(self, dt): @pytest.mark.parametrize("dt", get_float_dtypes()) def test_singlecomplex(self, dt): - a = numpy.random.rand(10).astype(dt) + a = generate_random_numpy_array(10, dtype=dt) ia = dpnp.array(a) result = dpnp.real_if_close(ia + 1e-7j) @@ -1710,7 +1585,7 @@ def test_singlecomplex(self, dt): @pytest.mark.parametrize("dt", get_float_dtypes()) def test_tol(self, dt): - a = numpy.random.rand(10).astype(dt) + a = generate_random_numpy_array(10, dtype=dt) ia = dpnp.array(a) result = dpnp.real_if_close(ia + 1e-7j, tol=1e-6) @@ -1729,7 +1604,6 @@ class TestSinc: "dt", get_all_dtypes(no_none=True, no_bool=True, no_float16=False) ) def test_basic(self, dt): - low = 0 if dpnp.issubdtype(dt, dpnp.integer) else -1 a = numpy.linspace(-1, 1, 100, dtype=dt) ia = dpnp.array(a) @@ -1834,15 +1708,14 @@ def test_zeros(self, dt): result = dpnp.spacing(ia) expected = numpy.spacing(a) - tol = numpy.finfo(expected.dtype).resolution if numpy_version() < "2.0.0": - assert_allclose(result, expected, rtol=tol, atol=tol) + assert_allclose(result, expected) else: # numpy.spacing(-0.0) == numpy.spacing(0.0), i.e. NumPy returns # positive value (looks as a bug in NumPy), because for any other # negative input the NumPy result will be also a negative value. expected[1] *= -1 - assert_allclose(result, expected, rtol=tol, atol=tol) + assert_allclose(result, expected) @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) @pytest.mark.parametrize("val", [1, 1e-5, 1000]) @@ -1903,7 +1776,7 @@ def get_numpy_func(self): return numpy.trapz return numpy.trapezoid - @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize("dt", get_all_dtypes(no_none=True, no_bool=True)) @pytest.mark.parametrize( "data", [[1, 2, 3], [[1, 2, 3], [4, 5, 6]], [1, 4, 6, 9, 10, 12], [], [1]], @@ -1927,10 +1800,10 @@ def test_simple(self, dt): expected = self.get_numpy_func()( numpy.exp(-0.5 * x**2) / sqrt_2pi, dx=0.1 ) - assert_almost_equal(result, expected, 7) + assert_allclose(result, expected, rtol=1e-6) - @pytest.mark.parametrize("y_dt", get_all_dtypes(no_bool=True)) - @pytest.mark.parametrize("x_dt", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize("y_dt", get_all_dtypes(no_none=True, no_bool=True)) + @pytest.mark.parametrize("x_dt", get_all_dtypes(no_none=True, no_bool=True)) @pytest.mark.parametrize("y_arr", [[1, 2, 4, 5], [1.0, 2.5, 6.0, 7.0]]) @pytest.mark.parametrize("x_arr", [[2, 5, 6, 9]]) def test_x_samples(self, y_arr, x_arr, y_dt, x_dt): @@ -1940,7 +1813,7 @@ def test_x_samples(self, y_arr, x_arr, y_dt, x_dt): result = dpnp.trapezoid(iy, ix) expected = self.get_numpy_func()(y, x) - assert_array_equal(expected, result) + assert_array_equal(result, expected) @pytest.mark.parametrize("data", [[1, 2, 3], [4, 5, 6]]) def test_2d_with_x_samples(self, data): @@ -1949,7 +1822,7 @@ def test_2d_with_x_samples(self, data): result = dpnp.trapezoid(ia, ia) expected = self.get_numpy_func()(a, a) - assert_array_equal(expected, result) + assert_array_equal(result, expected) @pytest.mark.parametrize( "data", @@ -1970,7 +1843,7 @@ def test_dx_samples(self, data, dx): result = dpnp.trapezoid(iy, dx=dx) expected = self.get_numpy_func()(y, dx=dx) - assert_array_equal(expected, result) + assert_array_equal(result, expected) def test_ndim(self): x = numpy.linspace(0, 1, 3) @@ -2091,14 +1964,12 @@ def test_discont(self, dt): @pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings") -@pytest.mark.parametrize( - "val_type", [bool, int, float], ids=["bool", "int", "float"] -) -@pytest.mark.parametrize("data_type", get_all_dtypes()) +@pytest.mark.parametrize("val_type", [bool, int, float]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "func", ["add", "divide", "multiply", "power", "subtract"] ) -@pytest.mark.parametrize("val", [0, 1, 5], ids=["0", "1", "5"]) +@pytest.mark.parametrize("val", [0, 1, 5]) @pytest.mark.parametrize( "array", [ @@ -2111,165 +1982,153 @@ def test_discont(self, dt): [[[1, 3], [3, 1]], [[0, 1], [1, 3]]], ], ], - ids=[ - "[[0, 0], [0, 0]]", - "[[1, 2], [1, 2]]", - "[[1, 2], [3, 4]]", - "[[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]", - "[[[[1, 2], [3, 4]], [[1, 2], [2, 1]]], [[[1, 3], [3, 1]], [[0, 1], [1, 3]]]]", - ], + ids=["2D-zeros", "2D-repetitive", "2D", "3D", "4D"], ) -def test_op_with_scalar(array, val, func, data_type, val_type): - np_a = numpy.array(array, dtype=data_type) - dpnp_a = dpnp.array(array, dtype=data_type) +def test_op_with_scalar(array, val, func, dtype, val_type): + a = numpy.array(array, dtype=dtype) + ia = dpnp.array(a) val_ = val_type(val) if func == "power": if ( val_ == 0 - and numpy.issubdtype(data_type, numpy.complexfloating) - and not dpnp.all(dpnp_a) + and numpy.issubdtype(dtype, numpy.complexfloating) + and not dpnp.all(ia) ): pytest.skip( "(0j ** 0) is different: (NaN + NaNj) in dpnp and (1 + 0j) in numpy" ) - if func == "subtract" and val_type == bool and data_type == dpnp.bool: + if func == "subtract" and val_type == bool and dtype == dpnp.bool: with pytest.raises(TypeError): - result = getattr(dpnp, func)(dpnp_a, val_) - expected = getattr(numpy, func)(np_a, val_) + result = getattr(dpnp, func)(ia, val_) + expected = getattr(numpy, func)(a, val_) - result = getattr(dpnp, func)(val_, dpnp_a) - expected = getattr(numpy, func)(val_, np_a) + result = getattr(dpnp, func)(val_, ia) + expected = getattr(numpy, func)(val_, a) else: - result = getattr(dpnp, func)(dpnp_a, val_) - expected = getattr(numpy, func)(np_a, val_) - assert_allclose(result, expected, rtol=1e-6) + result = getattr(dpnp, func)(ia, val_) + expected = getattr(numpy, func)(a, val_) + assert_allclose(result, expected, rtol=1e-06) - result = getattr(dpnp, func)(val_, dpnp_a) - expected = getattr(numpy, func)(val_, np_a) - assert_allclose(result, expected, rtol=1e-6) + result = getattr(dpnp, func)(val_, ia) + expected = getattr(numpy, func)(val_, a) + assert_allclose(result, expected) -@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["()", "(3, 2)"]) -@pytest.mark.parametrize("dtype", get_all_dtypes()) +@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_multiply_scalar(shape, dtype): - np_a = numpy.ones(shape, dtype=dtype) - dpnp_a = dpnp.ones(shape, dtype=dtype) + a = numpy.ones(shape, dtype=dtype) + ia = dpnp.array(a) - result = 0.5 * dpnp_a * 1.7 - expected = 0.5 * np_a * 1.7 + result = 0.5 * ia * 1.7 + expected = 0.5 * a * 1.7 assert_allclose(result, expected) -@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["()", "(3, 2)"]) -@pytest.mark.parametrize("dtype", get_all_dtypes()) +@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_add_scalar(shape, dtype): - np_a = numpy.ones(shape, dtype=dtype) - dpnp_a = dpnp.ones(shape, dtype=dtype) + a = numpy.ones(shape, dtype=dtype) + ia = dpnp.array(a) - result = 0.5 + dpnp_a + 1.7 - expected = 0.5 + np_a + 1.7 + result = 0.5 + ia + 1.7 + expected = 0.5 + a + 1.7 assert_allclose(result, expected) -@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["()", "(3, 2)"]) -@pytest.mark.parametrize("dtype", get_all_dtypes()) +@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_subtract_scalar(shape, dtype): - np_a = numpy.ones(shape, dtype=dtype) - dpnp_a = dpnp.ones(shape, dtype=dtype) + a = numpy.ones(shape, dtype=dtype) + ia = dpnp.array(a) - result = 0.5 - dpnp_a - 1.7 - expected = 0.5 - np_a - 1.7 + result = 0.5 - ia - 1.7 + expected = 0.5 - a - 1.7 assert_allclose(result, expected) -@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["()", "(3, 2)"]) -@pytest.mark.parametrize("dtype", get_all_dtypes()) +@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_divide_scalar(shape, dtype): - np_a = numpy.ones(shape, dtype=dtype) - dpnp_a = dpnp.ones(shape, dtype=dtype) + a = numpy.ones(shape, dtype=dtype) + ia = dpnp.array(a) - result = 0.5 / dpnp_a / 1.7 - expected = 0.5 / np_a / 1.7 - assert_allclose(result, expected, rtol=1e-6) + result = 0.5 / ia / 1.7 + expected = 0.5 / a / 1.7 + assert_allclose(result, expected, rtol=1e-06) @pytest.mark.parametrize( - "data", - [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], - ids=["[[1., -1.], [0.1, -0.1]]", "[-2, -1, 0, 1, 2]"], + "data", [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], ids=["2D", "1D"] ) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_unsigned=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_unsigned=True) ) def test_negative(data, dtype): - np_a = numpy.array(data, dtype=dtype) - dpnp_a = dpnp.array(np_a) + a = numpy.array(data, dtype=dtype) + ia = dpnp.array(a) - result = dpnp.negative(dpnp_a) - expected = numpy.negative(np_a) + result = dpnp.negative(ia) + expected = numpy.negative(a) assert_allclose(result, expected) - result = -dpnp_a - expected = -np_a - assert_allclose(result, expected) + result = -ia + expected = -a + assert_dtype_allclose(result, expected) # out keyword if dtype is not None: - dp_out = dpnp.empty(expected.shape, dtype=dtype) - result = dpnp.negative(dpnp_a, out=dp_out) - assert result is dp_out + iout = dpnp.empty(expected.shape, dtype=dtype) + result = dpnp.negative(ia, out=iout) + assert result is iout assert_allclose(result, expected) def test_negative_boolean(): - dpnp_a = dpnp.array([True, False]) + ia = dpnp.array([True, False]) with pytest.raises(TypeError): - dpnp.negative(dpnp_a) + dpnp.negative(ia) @pytest.mark.parametrize( - "data", - [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], - ids=["[[1., -1.], [0.1, -0.1]]", "[-2, -1, 0, 1, 2]"], + "data", [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], ids=["2D", "1D"] ) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True)) def test_positive(data, dtype): - np_a = get_abs_array(data, dtype=dtype) - dpnp_a = dpnp.array(np_a) + a = get_abs_array(data, dtype=dtype) + ia = dpnp.array(a) - result = dpnp.positive(dpnp_a) - expected = numpy.positive(np_a) + result = dpnp.positive(ia) + expected = numpy.positive(a) assert_allclose(result, expected) - result = +dpnp_a - expected = +np_a + result = +ia + expected = +a assert_allclose(result, expected) # out keyword if dtype is not None: - dp_out = dpnp.empty(expected.shape, dtype=dtype) - result = dpnp.positive(dpnp_a, out=dp_out) - assert result is dp_out + iout = dpnp.empty(expected.shape, dtype=dtype) + result = dpnp.positive(ia, out=iout) + assert result is iout assert_allclose(result, expected) def test_positive_boolean(): - dpnp_a = dpnp.array([True, False]) + ia = dpnp.array([True, False]) with pytest.raises(TypeError): - dpnp.positive(dpnp_a) + dpnp.positive(ia) @pytest.mark.parametrize("dtype", get_float_dtypes(no_float16=False)) def test_float_remainder_magnitude(dtype): b = numpy.array(1.0, dtype=dtype) a = numpy.nextafter(numpy.array(0.0, dtype=dtype), -b) - - ia = dpnp.array(a) - ib = dpnp.array(b) + ia, ib = dpnp.array(a), dpnp.array(b) result = dpnp.remainder(ia, ib) expected = numpy.remainder(a, b) @@ -2300,9 +2159,7 @@ def test_float_remainder_magnitude(dtype): def test_float_remainder_fmod_nans_inf(func, dtype, lhs, rhs): a = numpy.array(lhs, dtype=dtype) b = numpy.array(rhs, dtype=dtype) - - ia = dpnp.array(a) - ib = dpnp.array(b) + ia, ib = dpnp.array(a), dpnp.array(b) result = getattr(dpnp, func)(ia, ib) expected = getattr(numpy, func)(a, b) @@ -2338,25 +2195,25 @@ def test_sign(dtype): ids=["[2, 0, -2]", "[1.1, -1.1]"], ) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_unsigned=True) + "dtype", get_all_dtypes(no_none=True, no_complex=True, no_unsigned=True) ) def test_signbit(data, dtype): - np_a = numpy.array(data, dtype=dtype) - dpnp_a = dpnp.array(data, dtype=dtype) + a = numpy.array(data, dtype=dtype) + ia = dpnp.array(a) - result = dpnp.signbit(dpnp_a) - expected = numpy.signbit(np_a) + result = dpnp.signbit(ia) + expected = numpy.signbit(a) assert_dtype_allclose(result, expected) # out keyword - dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) - result = dpnp.signbit(dpnp_a, out=dp_out) - assert dp_out is result + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.signbit(ia, out=iout) + assert iout is result assert_dtype_allclose(result, expected) class TestRealImag: - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_real_imag(self, dtype): a = generate_random_numpy_array(20, dtype) ia = dpnp.array(a) @@ -2399,19 +2256,19 @@ def test_projection_infinity(self, dtype): a = dpnp.array(X, dtype=dtype) result = dpnp.proj(a) expected = dpnp.array(Y, dtype=dtype) - assert_dtype_allclose(result, expected) + assert dpnp.allclose(result, expected) # out keyword - dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) - result = dpnp.proj(a, out=dp_out) - assert dp_out is result - assert_dtype_allclose(result, expected) + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.proj(a, out=iout) + assert iout is result + assert dpnp.allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_projection(self, dtype): result = dpnp.proj(dpnp.array(1, dtype=dtype)) expected = dpnp.array(complex(1, 0)) - assert_allclose(result, expected) + assert dpnp.allclose(result, expected) class TestRoundingFuncs: @@ -2421,34 +2278,30 @@ class TestRoundingFuncs: {"func_name": "floor", "input_values": [-5, 5, 10]}, {"func_name": "trunc", "input_values": [-5, 5, 10]}, ], - ids=[ - "ceil", - "floor", - "trunc", - ], + ids=["ceil", "floor", "trunc"], ) def func_params(self, request): return request.param - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) def test_out(self, func_params, dtype): func_name = func_params["func_name"] input_values = func_params["input_values"] - np_array, expected = _get_numpy_arrays_1in_1out( - func_name, dtype, input_values - ) + a, expected = _get_numpy_arrays_1in_1out(func_name, dtype, input_values) - dp_array = dpnp.array(np_array) - out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = getattr(dpnp, func_name)(dp_array, out=dp_out) + ia = dpnp.array(a) + out_dt = numpy.int8 if dtype == dpnp.bool else dtype + iout = dpnp.empty(expected.shape, dtype=out_dt) + result = getattr(dpnp, func_name)(ia, out=iout) - assert result is dp_out + assert result is iout # numpy.ceil, numpy.floor, numpy.trunc always return float dtype for # NumPy < 2.1.0 while output has the dtype of input for NumPy >= 2.1.0 # (dpnp follows the latter behavior except for boolean dtype where it # returns int8) - if numpy_version() < "2.1.0" or dtype == numpy.bool: + if numpy_version() < "2.1.0" or dtype == dpnp.bool: check_type = False else: check_type = True @@ -2460,63 +2313,55 @@ def test_out(self, func_params, dtype): def test_invalid_dtype(self, func_params, dtype): func_name = func_params["func_name"] dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) + ia = dpnp.arange(10, dtype=dpnp_dtype) + iout = dpnp.empty(10, dtype=dtype) with pytest.raises(ValueError): - getattr(dpnp, func_name)(dp_array, out=dp_out) + getattr(dpnp, func_name)(ia, out=iout) @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] + "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15,)", "(2, 2)"] ) def test_invalid_shape(self, func_params, shape): func_name = func_params["func_name"] - dp_array = dpnp.arange(10, dtype=dpnp.float32) - dp_out = dpnp.empty(shape, dtype=dpnp.float32) - - with pytest.raises(ValueError): - getattr(dpnp, func_name)(dp_array, out=dp_out) + ia = dpnp.arange(10, dtype=dpnp.float32) + iout = dpnp.empty(shape, dtype=dpnp.float32) + assert_raises(ValueError, getattr(dpnp, func_name), ia, out=iout) class TestHypot: @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_hypot(self, dtype): - np_array1, np_array2, expected = _get_numpy_arrays_2in_1out( - "hypot", dtype, [0, 10, 10] - ) + a, b, expected = _get_numpy_arrays_2in_1out("hypot", dtype, [0, 10, 10]) - dp_array1 = dpnp.array(np_array1) - dp_array2 = dpnp.array(np_array2) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.hypot(dp_array1, dp_array2, out=dp_out) + ia, ib = dpnp.array(a), dpnp.array(b) + out_dt = _get_output_data_type(dtype) + iout = dpnp.empty(expected.shape, dtype=out_dt) + result = dpnp.hypot(ia, ib, out=iout) - assert result is dp_out + assert result is iout assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_dtypes()) def test_out_overlap(self, dtype): size = 15 - dp_a = dpnp.arange(2 * size, dtype=dtype) - dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:]) + ia = dpnp.arange(2 * size, dtype=dtype) + dpnp.hypot(ia[size::], ia[::2], out=ia[:size:]) - np_a = numpy.arange(2 * size, dtype=dtype) - numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:]) + a = numpy.arange(2 * size, dtype=dtype) + numpy.hypot(a[size::], a[::2], out=a[:size:]) - assert_dtype_allclose(dp_a, np_a) + assert_dtype_allclose(ia, a) @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] + "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15,)", "(2, 2)"] ) def test_invalid_shape(self, shape): - dp_array1 = dpnp.arange(10) - dp_array2 = dpnp.arange(10) - dp_out = dpnp.empty(shape) - - with pytest.raises(ValueError): - dpnp.hypot(dp_array1, dp_array2, out=dp_out) + ia = dpnp.arange(10) + iout = dpnp.empty(shape) + assert_raises(ValueError, dpnp.hypot, ia, ia, iout) @pytest.mark.parametrize("xp", [dpnp, numpy]) @pytest.mark.parametrize( @@ -2530,10 +2375,12 @@ def test_invalid_out(self, xp, out): class TestLogSumExp: - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) @pytest.mark.parametrize("axis", [None, 2, -1, (0, 1)]) @pytest.mark.parametrize("keepdims", [True, False]) - def test_logsumexp(self, dtype, axis, keepdims): + def test_basic(self, dtype, axis, keepdims): a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype) res = dpnp.logsumexp(a, axis=axis, keepdims=keepdims) @@ -2548,77 +2395,52 @@ def test_logsumexp(self, dtype, axis, keepdims): assert_dtype_allclose(res, exp) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) - @pytest.mark.parametrize("axis", [None, 2, -1, (0, 1)]) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_logsumexp_out(self, dtype, axis, keepdims): - a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype) - exp_dt = None - dtype_list = [dpnp.bool, dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16] - if dtype in dtype_list: - exp_dt = dpnp.default_float_type(a.device) - exp = numpy.logaddexp.reduce( - dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dt - ) - - exp_dt = exp.dtype - if exp_dt == numpy.float64 and not has_support_aspect64(): - exp_dt = numpy.float32 - dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dt) - res = dpnp.logsumexp(a, axis=axis, out=dpnp_out, keepdims=keepdims) - - assert res is dpnp_out - assert_dtype_allclose(res, exp) - @pytest.mark.parametrize( - "in_dtype", get_all_dtypes(no_bool=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) - @pytest.mark.parametrize("out_dtype", get_all_dtypes(no_bool=True)) - def test_logsumexp_dtype(self, in_dtype, out_dtype): - a = dpnp.ones(100, dtype=in_dtype) - res = dpnp.logsumexp(a, dtype=out_dtype) - exp = numpy.logaddexp.reduce(dpnp.asnumpy(a)) - exp = exp.astype(out_dtype) + @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True)) + def test_dtype(self, in_dt, dt): + a = dpnp.ones(100, dtype=in_dt) + res = dpnp.logsumexp(a, dtype=dt) + exp = numpy.logaddexp.reduce(dpnp.asnumpy(a)).astype(res.dtype) dtype_list = [dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16] - tol = 1e-2 if in_dtype in dtype_list else 1e-6 + tol = 1e-2 if in_dt in dtype_list else 1e-6 assert_allclose(res, exp, rtol=tol) @testing.with_requires("numpy>=1.26.4") @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") @pytest.mark.parametrize( - "arr_dt", get_all_dtypes(no_none=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_complex=True) ) @pytest.mark.parametrize( "out_dt", get_all_dtypes(no_none=True, no_complex=True) ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_logsumexp_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(1, 11).reshape((2, 5)).astype(dtype=arr_dt) + def test_out(self, in_dt, out_dt): + a = numpy.arange(1, 11).reshape(2, 5).astype(dtype=in_dt) out = numpy.zeros_like(a, shape=(2,), dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = dpnp.logsumexp(ia, out=iout, dtype=dtype, axis=1) + result = dpnp.logsumexp(ia, out=iout, axis=1) + assert result is iout if numpy.issubdtype(out_dt, numpy.uint64): # NumPy returns incorrect results for this case if out kwarg is used - exp = numpy.logaddexp.reduce(a, axis=1) - exp = exp.astype(out_dt) + exp = numpy.logaddexp.reduce(a, axis=1).astype(out_dt) else: exp = numpy.logaddexp.reduce(a, out=out, axis=1) dtype_list = [dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16] - tol = 1e-2 if arr_dt in dtype_list else 1e-6 - assert_allclose(result, exp.astype(dtype), rtol=tol) - assert result is iout + tol = 1e-2 if in_dt in dtype_list else 1e-6 + assert_allclose(result, exp, rtol=tol) class TestReduceHypot: - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) @pytest.mark.parametrize("axis", [None, 2, -1, (0, 1)]) @pytest.mark.parametrize("keepdims", [True, False]) - def test_reduce_hypot(self, dtype, axis, keepdims): + def test_basic(self, dtype, axis, keepdims): a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype) res = dpnp.reduce_hypot(a, axis=axis, keepdims=keepdims) @@ -2633,60 +2455,35 @@ def test_reduce_hypot(self, dtype, axis, keepdims): assert_dtype_allclose(res, exp) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) - @pytest.mark.parametrize("axis", [None, 2, -1, (0, 1)]) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_reduce_hypot_out(self, dtype, axis, keepdims): - a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype) - exp_dt = None - dtype_list = [dpnp.bool, dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16] - if dtype in dtype_list: - exp_dt = dpnp.default_float_type(a.device) - exp = numpy.hypot.reduce( - dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dt - ) - - exp_dt = exp.dtype - if exp_dt == numpy.float64 and not has_support_aspect64(): - exp_dt = numpy.float32 - dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dt) - res = dpnp.reduce_hypot(a, axis=axis, out=dpnp_out, keepdims=keepdims) - - assert res is dpnp_out - assert_dtype_allclose(res, exp) - @pytest.mark.parametrize( - "in_dtype", get_all_dtypes(no_bool=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) - @pytest.mark.parametrize("out_dtype", get_all_dtypes(no_bool=True)) - def test_reduce_hypot_dtype(self, in_dtype, out_dtype): - a = dpnp.ones(99, dtype=in_dtype) - res = dpnp.reduce_hypot(a, dtype=out_dtype) - exp = numpy.hypot.reduce(dpnp.asnumpy(a)) - exp = exp.astype(out_dtype) + @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True)) + def test_dtype(self, in_dt, dt): + a = dpnp.ones(99, dtype=in_dt) + res = dpnp.reduce_hypot(a, dtype=dt) + exp = numpy.hypot.reduce(dpnp.asnumpy(a)).astype(res.dtype) dtype_list = [dpnp.int8, dpnp.uint8] - tol = 1e-2 if in_dtype in dtype_list else 1e-6 + tol = 1e-2 if in_dt in dtype_list else 1e-6 assert_allclose(res, exp, rtol=tol) @pytest.mark.parametrize( - "arr_dt", get_all_dtypes(no_none=True, no_complex=True) + "in_dt", get_all_dtypes(no_none=True, no_complex=True) ) @pytest.mark.parametrize( "out_dt", get_all_dtypes(no_none=True, no_complex=True) ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_reduce_hypot_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(10, 20).reshape((2, 5)).astype(dtype=arr_dt) + def test_out(self, in_dt, out_dt): + a = numpy.arange(10, 20).reshape(2, 5).astype(dtype=in_dt) out = numpy.zeros_like(a, shape=(2,), dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = dpnp.reduce_hypot(ia, out=iout, dtype=dtype, axis=1) + result = dpnp.reduce_hypot(ia, out=iout, axis=1) exp = numpy.hypot.reduce(a, out=out, axis=1) - assert_allclose(result, exp.astype(dtype), rtol=1e-06) + assert result is iout + assert_allclose(result, exp, rtol=1e-06) @pytest.mark.parametrize( @@ -2694,13 +2491,13 @@ def test_reduce_hypot_out_dtype(self, arr_dt, out_dt, dtype): ) def test_inplace_remainder(dtype): size = 21 - np_a = numpy.arange(size, dtype=dtype) - dp_a = dpnp.arange(size, dtype=dtype) + a = numpy.arange(size, dtype=dtype) + ia = dpnp.arange(size, dtype=dtype) - np_a %= 4 - dp_a %= 4 + a %= 4 + ia %= 4 - assert_allclose(dp_a, np_a) + assert_allclose(ia, a) @pytest.mark.parametrize( @@ -2708,13 +2505,13 @@ def test_inplace_remainder(dtype): ) def test_inplace_floor_divide(dtype): size = 21 - np_a = numpy.arange(size, dtype=dtype) - dp_a = dpnp.arange(size, dtype=dtype) + a = numpy.arange(size, dtype=dtype) + ia = dpnp.arange(size, dtype=dtype) - np_a //= 4 - dp_a //= 4 + a //= 4 + ia //= 4 - assert_allclose(dp_a, np_a) + assert_allclose(ia, a) def test_elemenwise_nin_nout(): @@ -2792,21 +2589,11 @@ def test_bitwise_1array_input(): @pytest.mark.parametrize( "x_shape", - [ - (), - (2), - (3, 4), - (3, 4, 5), - ], + [(), (2), (3, 4), (3, 4, 5)], ) @pytest.mark.parametrize( "y_shape", - [ - (), - (2), - (3, 4), - (3, 4, 5), - ], + [(), (2), (3, 4), (3, 4, 5)], ) def test_elemenwise_outer(x_shape, y_shape): x_np = numpy.random.random(x_shape) @@ -2829,4 +2616,4 @@ def test_elemenwise_outer_scalar(): y = dpnp.asarray(s) expected = dpnp.add.outer(x, y) result = dpnp.add.outer(x, s) - assert_dtype_allclose(result, expected) + assert dpnp.allclose(result, expected)