diff --git a/dpnp/tests/helper.py b/dpnp/tests/helper.py index 90a6eb54619..ee457670f59 100644 --- a/dpnp/tests/helper.py +++ b/dpnp/tests/helper.py @@ -162,7 +162,7 @@ def get_all_dtypes( def generate_random_numpy_array( - shape, dtype=None, hermitian=False, seed_value=None + shape, dtype=None, hermitian=False, seed_value=None, low=-10, high=10 ): """ Generate a random numpy array with the specified shape and dtype. @@ -177,13 +177,19 @@ def generate_random_numpy_array( dtype : str or dtype, optional Desired data-type for the output array. If not specified, data type will be determined by numpy. - Default : None + Default : ``None`` hermitian : bool, optional If True, generates a Hermitian (symmetric if `dtype` is real) matrix. - Default : False + Default : ``False`` seed_value : int, optional The seed value to initialize the random number generator. - Default : None + Default : ``None`` + low : {int, float}, optional + Lower boundary of the generated samples from a uniform distribution. + Default : ``-10``. + high : {int, float}, optional + Upper boundary of the generated samples from a uniform distribution. + Default : ``10``. Returns ------- @@ -197,13 +203,17 @@ def generate_random_numpy_array( """ + if seed_value is None: + seed_value = 42 numpy.random.seed(seed_value) - a = numpy.random.randn(*shape).astype(dtype) + # dtype=int is needed for 0d arrays + size = numpy.prod(shape, dtype=int) + a = numpy.random.uniform(low, high, size).astype(dtype) if numpy.issubdtype(a.dtype, numpy.complexfloating): - numpy.random.seed(seed_value) - a += 1j * numpy.random.randn(*shape) + a += 1j * numpy.random.uniform(low, high, size) + a = a.reshape(shape) if hermitian and a.size > 0: if a.ndim > 2: orig_shape = a.shape diff --git a/dpnp/tests/test_fft.py b/dpnp/tests/test_fft.py index 196b7a57345..8915daa43e2 100644 --- a/dpnp/tests/test_fft.py +++ b/dpnp/tests/test_fft.py @@ -10,6 +10,7 @@ from .helper import ( assert_dtype_allclose, + generate_random_numpy_array, get_all_dtypes, get_complex_dtypes, get_float_dtypes, @@ -24,11 +25,11 @@ def _make_array_Hermitian(a, n): """ - a[0].imag = 0 + a[0] = a[0].real if n in [None, 18]: # f_ny is Nyquist mode (n//2+1 mode) which is n//2 element f_ny = -1 if n is None else n // 2 - a[f_ny].imag = 0 + a[f_ny] = a[f_ny].real a[f_ny:] = 0 # no data needed after Nyquist mode return a @@ -48,7 +49,7 @@ def setup_method(self): @pytest.mark.parametrize( "shape", [(64,), (8, 8), (4, 16), (4, 4, 4), (2, 4, 4, 2)] ) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_fft_ndim(self, dtype, shape, norm): np_data = numpy.arange(64, dtype=dtype).reshape(shape) dpnp_data = dpnp.arange(64, dtype=dtype).reshape(shape) @@ -61,15 +62,13 @@ def test_fft_ndim(self, dtype, shape, norm): dpnp_res = dpnp.fft.ifft(dpnp_data, norm=norm) assert_dtype_allclose(dpnp_res, np_res, check_only_type_kind=True) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_fft_1D(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11, dtype=dtype) - a = dpnp.sin(x) # a.dtype is float16 if x.dtype is bool - a_np = dpnp.asnumpy(a) + x = generate_random_numpy_array(11, dtype, low=-1, high=1) + a_np = numpy.sin(x) # a.dtype is float16 if x.dtype is bool + a = dpnp.array(a_np) factor = 140 if dtype == dpnp.bool else 8 result = dpnp.fft.fft(a, n=n, norm=norm) @@ -84,7 +83,7 @@ def test_fft_1D(self, dtype, n, norm): iresult, iexpected, factor=factor, check_only_type_kind=True ) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_fft_1D_bool(self, norm): a = dpnp.linspace(-1, 1, 11, dtype=dpnp.bool) a_np = dpnp.asnumpy(a) @@ -97,27 +96,10 @@ def test_fft_1D_bool(self, norm): iexpected = numpy.fft.ifft(expected, norm=norm) assert_dtype_allclose(iresult, iexpected, check_only_type_kind=True) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) - def test_fft_1D_complex(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11) - a = dpnp.sin(x) + 1j * dpnp.cos(x) - a = dpnp.asarray(a, dtype=dtype) - a_np = dpnp.asnumpy(a) - - result = dpnp.fft.fft(a, n=n, norm=norm) - expected = numpy.fft.fft(a_np, n=n, norm=norm) - assert_dtype_allclose(result, expected, check_only_type_kind=True) - - iresult = dpnp.fft.ifft(result, n=n, norm=norm) - iexpected = numpy.fft.ifft(expected, n=n, norm=norm) - assert_dtype_allclose(iresult, iexpected, check_only_type_kind=True) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_fft_1D_on_2D_array(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -134,7 +116,7 @@ def test_fft_1D_on_2D_array(self, dtype, n, axis, norm, order): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [0, 1, 2]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_fft_1D_on_3D_array(self, dtype, n, axis, norm, order): x1 = numpy.random.uniform(-10, 10, 24) @@ -174,7 +156,7 @@ def test_fft_usm_ndarray(self, n): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_fft_1D_out(self, dtype, n, norm): x = dpnp.linspace(-1, 1, 11) a = dpnp.sin(x) + 1j * dpnp.cos(x) @@ -274,7 +256,7 @@ def test_fft_inplace_out(self, axis): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_fft_1D_on_2D_array_out(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -393,33 +375,13 @@ class TestFft2: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) - def test_fft2(self, dtype): - x1 = numpy.random.uniform(-10, 10, 24) - a_np = numpy.array(x1, dtype=dtype).reshape(2, 3, 4) - a = dpnp.asarray(a_np) - - result = dpnp.fft.fft2(a) - expected = numpy.fft.fft2(a_np) - assert_dtype_allclose(result, expected, check_only_type_kind=True) - - iresult = dpnp.fft.ifft2(result) - iexpected = numpy.fft.ifft2(expected) - assert_dtype_allclose(iresult, iexpected, check_only_type_kind=True) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("axes", [(0, 1), (1, 2), (0, 2), (2, 1), (2, 0)]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) - def test_fft2_complex(self, dtype, axes, norm, order): - x1 = numpy.random.uniform(-10, 10, 24) - x2 = numpy.random.uniform(-10, 10, 24) - a_np = numpy.array(x1 + 1j * x2, dtype=dtype).reshape( - 2, 3, 4, order=order - ) - a = dpnp.asarray(a_np) + def test_fft2(self, dtype, axes, norm, order): + a_np = generate_random_numpy_array((2, 3, 4), dtype) + a = dpnp.array(a_np) result = dpnp.fft.fft2(a, axes=axes, norm=norm) expected = numpy.fft.fft2(a_np, axes=axes, norm=norm) @@ -431,10 +393,8 @@ def test_fft2_complex(self, dtype, axes, norm, order): @pytest.mark.parametrize("s", [None, (3, 3), (10, 10), (3, 10)]) def test_fft2_s(self, s): - x1 = numpy.random.uniform(-10, 10, 48) - x2 = numpy.random.uniform(-10, 10, 48) - a_np = numpy.array(x1 + 1j * x2, dtype=numpy.complex64).reshape(6, 8) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((6, 8), dtype=numpy.complex64) + a = dpnp.array(a_np) result = dpnp.fft.fft2(a, s=s) expected = numpy.fft.fft2(a_np, s=s) @@ -473,19 +433,17 @@ class TestFftn: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_bool=True, no_none=True) + ) @pytest.mark.parametrize( "axes", [None, (0, 1, 2), (-1, -4, -2), (-2, -4, -1, -3)] ) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_fftn(self, dtype, axes, norm, order): - x1 = numpy.random.uniform(-10, 10, 120) - x2 = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x1 + 1j * x2, dtype=dtype).reshape( - 2, 3, 4, 5, order=order - ) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((2, 3, 4, 5), dtype) + a = dpnp.array(a_np) result = dpnp.fft.fftn(a, axes=axes, norm=norm) expected = numpy.fft.fftn(a_np, axes=axes, norm=norm) @@ -499,12 +457,8 @@ def test_fftn(self, dtype, axes, norm, order): "axes", [(2, 0, 2, 0), (0, 1, 1), (2, 0, 1, 3, 2, 1)] ) def test_fftn_repeated_axes(self, axes): - x1 = numpy.random.uniform(-10, 10, 120) - x2 = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x1 + 1j * x2, dtype=numpy.complex64).reshape( - 2, 3, 4, 5 - ) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64) + a = dpnp.array(a_np) result = dpnp.fft.fftn(a, axes=axes) # Intel® NumPy ignores repeated axes, handle it one by one @@ -522,12 +476,8 @@ def test_fftn_repeated_axes(self, axes): @pytest.mark.parametrize("axes", [(2, 3, 3, 2), (0, 0, 3, 3)]) @pytest.mark.parametrize("s", [(5, 4, 3, 3), (7, 8, 10, 9)]) def test_fftn_repeated_axes_with_s(self, axes, s): - x1 = numpy.random.uniform(-10, 10, 120) - x2 = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x1 + 1j * x2, dtype=numpy.complex64).reshape( - 2, 3, 4, 5 - ) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64) + a = dpnp.array(a_np) result = dpnp.fft.fftn(a, s=s, axes=axes) # Intel® NumPy ignores repeated axes, handle it one by one @@ -545,12 +495,8 @@ def test_fftn_repeated_axes_with_s(self, axes, s): @pytest.mark.parametrize("axes", [(0, 1, 2, 3), (1, 2, 1, 2), (2, 2, 2, 3)]) @pytest.mark.parametrize("s", [(2, 3, 4, 5), (5, 4, 7, 8), (2, 5, 1, 2)]) def test_fftn_out(self, axes, s): - x1 = numpy.random.uniform(-10, 10, 120) - x2 = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x1 + 1j * x2, dtype=numpy.complex64).reshape( - 2, 3, 4, 5 - ) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64) + a = dpnp.array(a_np) out_shape = list(a.shape) for s_i, axis in zip(s[::-1], axes[::-1]): @@ -573,9 +519,7 @@ def test_fftn_out(self, axes, s): assert_dtype_allclose(iresult, iexpected, check_only_type_kind=True) def test_negative_s(self): - x1 = numpy.random.uniform(-10, 10, 60) - x2 = numpy.random.uniform(-10, 10, 60) - a_np = numpy.array(x1 + 1j * x2, dtype=numpy.complex64).reshape(3, 4, 5) + a_np = generate_random_numpy_array((3, 4, 5), dtype=numpy.complex64) a = dpnp.array(a_np) # For dpnp and stock NumPy 2.0, if s is -1, the whole input is used @@ -672,15 +616,15 @@ class TestHfft: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) - @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("n", [None, 5, 18]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_hfft_1D(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11, dtype=dtype) - a = dpnp.sin(x) - a_np = dpnp.asnumpy(a) + x = generate_random_numpy_array(11, dtype, low=-1, high=1) + a_np = numpy.sin(x) + if numpy.issubdtype(dtype, numpy.complexfloating): + a_np = _make_array_Hermitian(a_np, n) + a = dpnp.array(a_np) result = dpnp.fft.hfft(a, n=n, norm=norm) expected = numpy.fft.hfft(a_np, n=n, norm=norm) @@ -688,36 +632,22 @@ def test_hfft_1D(self, dtype, n, norm): # but dpnp return float32 if input is float32 assert_dtype_allclose(result, expected, check_only_type_kind=True) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize("n", [None, 5, 18]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) - def test_hfft_1D_complex(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11) - a = dpnp.sin(x) + 1j * dpnp.cos(x) - a = _make_array_Hermitian(a, n) - a = dpnp.asarray(a, dtype=dtype) - a_np = dpnp.asnumpy(a) - - result = dpnp.fft.hfft(a, n=n, norm=norm) - expected = numpy.fft.hfft(a_np, n=n, norm=norm) - assert_dtype_allclose(result, expected, check_only_type_kind=True) - @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True) ) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_ihfft_1D(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11, dtype=dtype) - a = dpnp.sin(x) - a_np = dpnp.asnumpy(a) + x = generate_random_numpy_array(11, dtype, low=-1, high=1) + a_np = numpy.sin(x) + a = dpnp.array(a_np) result = dpnp.fft.ihfft(a, n=n, norm=norm) expected = numpy.fft.ihfft(a_np, n=n, norm=norm) assert_dtype_allclose(result, expected, check_only_type_kind=True) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_ihfft_bool(self, n, norm): a = dpnp.ones(11, dtype=dpnp.bool) a_np = dpnp.asnumpy(a) @@ -731,15 +661,15 @@ class TestIrfft: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) - @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("n", [None, 5, 18]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_irfft_1D(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11, dtype=dtype) - a = dpnp.sin(x) - a_np = dpnp.asnumpy(a) + x = generate_random_numpy_array(11, dtype, low=-1, high=1) + a_np = numpy.sin(x) + if numpy.issubdtype(dtype, numpy.complexfloating): + a_np = _make_array_Hermitian(a_np, n) + a = dpnp.array(a_np) result = dpnp.fft.irfft(a, n=n, norm=norm) expected = numpy.fft.irfft(a_np, n=n, norm=norm) @@ -747,24 +677,10 @@ def test_irfft_1D(self, dtype, n, norm): # but dpnp return float32 if input is float32 assert_dtype_allclose(result, expected, check_only_type_kind=True) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize("n", [None, 5, 18]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) - def test_irfft_1D_complex(self, dtype, n, norm): - x = dpnp.linspace(-1, 1, 11) - a = dpnp.sin(x) + 1j * dpnp.cos(x) - a = _make_array_Hermitian(a, n) - a = dpnp.asarray(a, dtype=dtype) - a_np = dpnp.asnumpy(a) - - result = dpnp.fft.irfft(a, n=n, norm=norm) - expected = numpy.fft.irfft(a_np, n=n, norm=norm) - assert_dtype_allclose(result, expected, check_only_type_kind=True) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_irfft_1D_on_2D_array(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -777,14 +693,11 @@ def test_irfft_1D_on_2D_array(self, dtype, n, axis, norm, order): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [0, 1, 2]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_irfft_1D_on_3D_array(self, dtype, n, axis, norm, order): - x1 = numpy.random.uniform(-10, 10, 120) - x2 = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x1 + 1j * x2, dtype=dtype).reshape( - 4, 5, 6, order=order - ) + x = generate_random_numpy_array((4, 5, 6), dtype) + a_np = numpy.array(x, order=order) # each 1-D array of input should be Hermitian if axis == 0: a_np[0].imag = 0 @@ -830,7 +743,7 @@ def test_irfft_usm_ndarray(self, n): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 18]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_irfft_1D_out(self, dtype, n, norm): x = dpnp.linspace(-1, 1, 11) a = dpnp.sin(x) + 1j * dpnp.cos(x) @@ -849,7 +762,7 @@ def test_irfft_1D_out(self, dtype, n, norm): @pytest.mark.parametrize("dtype", get_complex_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_irfft_1D_on_2D_array_out(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -895,7 +808,7 @@ def test_rfft(self, dtype, shape): "dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True) ) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_rfft_1D(self, dtype, n, norm): x = dpnp.linspace(-1, 1, 11, dtype=dtype) a = dpnp.sin(x) @@ -906,7 +819,7 @@ def test_rfft_1D(self, dtype, n, norm): assert_dtype_allclose(result, expected, check_only_type_kind=True) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_rfft_bool(self, n, norm): a = dpnp.ones(11, dtype=dpnp.bool) a_np = dpnp.asnumpy(a) @@ -918,7 +831,7 @@ def test_rfft_bool(self, n, norm): @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_rfft_1D_on_2D_array(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -931,7 +844,7 @@ def test_rfft_1D_on_2D_array(self, dtype, n, axis, norm, order): @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [0, 1, 2]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_rfft_1D_on_3D_array(self, dtype, n, axis, norm, order): a_np = numpy.arange(24, dtype=dtype).reshape(2, 3, 4, order=order) @@ -957,7 +870,7 @@ def test_rfft_usm_ndarray(self, n): @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize("n", [None, 5, 20]) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) def test_rfft_1D_out(self, dtype, n, norm): x = dpnp.linspace(-1, 1, 11) a = dpnp.sin(x) + 1j * dpnp.cos(x) @@ -976,7 +889,7 @@ def test_rfft_1D_out(self, dtype, n, norm): @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize("n", [None, 5, 8]) @pytest.mark.parametrize("axis", [-1, 0]) - @pytest.mark.parametrize("norm", [None, "forward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_rfft_1D_on_2D_array_out(self, dtype, n, axis, norm, order): a_np = numpy.arange(12, dtype=dtype).reshape(3, 4, order=order) @@ -1018,11 +931,11 @@ def setup_method(self): "dtype", get_all_dtypes(no_none=True, no_complex=True) ) @pytest.mark.parametrize("axes", [(0, 1)]) # (1, 2),(0, 2),(2, 1),(2, 0) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_rfft2(self, dtype, axes, norm, order): - x = numpy.random.uniform(-10, 10, 24) - a_np = numpy.array(x, dtype=dtype).reshape(2, 3, 4, order=order) + x = generate_random_numpy_array((2, 3, 4), dtype) + a_np = numpy.array(x, order=order) a = dpnp.asarray(a_np) result = dpnp.fft.rfft2(a, axes=axes, norm=norm) @@ -1047,9 +960,8 @@ def test_irfft2(self, dtype): @pytest.mark.parametrize("s", [None, (3, 3), (10, 10), (3, 10)]) def test_rfft2_s(self, s): - x = numpy.random.uniform(-10, 10, 48) - a_np = numpy.array(x, dtype=numpy.float32).reshape(6, 8) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((6, 8)) + a = dpnp.array(a_np) result = dpnp.fft.rfft2(a, s=s) expected = numpy.fft.rfft2(a_np, s=s) @@ -1078,15 +990,17 @@ def setup_method(self): numpy.random.seed(42) # TODO: add additional axes when mkl_fft gh-119 is addressed - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_complex=True) + ) @pytest.mark.parametrize( "axes", [(0, 1, 2), (-2, -4, -1, -3)] # (-1, -4, -2) ) - @pytest.mark.parametrize("norm", ["forward", "backward", "ortho"]) + @pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"]) @pytest.mark.parametrize("order", ["C", "F"]) def test_rfftn(self, dtype, axes, norm, order): - x = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x, dtype=dtype).reshape(2, 3, 4, 5, order=order) + x = generate_random_numpy_array((2, 3, 4, 5), dtype) + a_np = numpy.array(x, order=order) a = dpnp.asarray(a_np) result = dpnp.fft.rfftn(a, axes=axes, norm=norm) @@ -1104,9 +1018,8 @@ def test_rfftn(self, dtype, axes, norm, order): "axes", [(2, 0, 2, 0), (0, 1, 1), (2, 0, 1, 3, 2, 1)] ) def test_rfftn_repeated_axes(self, axes): - x = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x, dtype=numpy.float32).reshape(2, 3, 4, 5) - a = dpnp.asarray(a_np) + a_np = generate_random_numpy_array((2, 3, 4, 5)) + a = dpnp.array(a_np) result = dpnp.fft.rfftn(a, axes=axes) # Intel® NumPy ignores repeated axes, handle it one by one @@ -1130,8 +1043,7 @@ def test_rfftn_repeated_axes(self, axes): @pytest.mark.parametrize("axes", [(2, 3, 3, 2), (0, 0, 3, 3)]) @pytest.mark.parametrize("s", [(5, 4, 3, 3), (7, 8, 10, 9)]) def test_rfftn_repeated_axes_with_s(self, axes, s): - x = numpy.random.uniform(-10, 10, 120) - a_np = numpy.array(x, dtype=numpy.float32).reshape(2, 3, 4, 5) + a_np = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.float32) a = dpnp.asarray(a_np) result = dpnp.fft.rfftn(a, s=s, axes=axes) diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index 754c4bf4618..810258d3f3f 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -294,9 +294,7 @@ def test_empty(self, shape, p): expected = numpy.linalg.cond(a, p=p) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( "shape", [(4, 4), (2, 4, 3, 3)], ids=["(4, 4)", "(2, 4, 3, 3)"] ) @@ -304,26 +302,7 @@ def test_empty(self, shape, p): "p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"] ) def test_basic(self, dtype, shape, p): - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape)), dtype=dtype - ).reshape(shape) - ia = dpnp.array(a) - - result = dpnp.linalg.cond(ia, p=p) - expected = numpy.linalg.cond(a, p=p) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape", [(4, 4), (2, 4, 3, 3)], ids=["(4, 4)", "(2, 4, 3, 3)"] - ) - @pytest.mark.parametrize( - "p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"] - ) - def test_complex(self, dtype, shape, p): - x1 = numpy.random.uniform(-5, 5, numpy.prod(shape)) - x2 = numpy.random.uniform(-5, 5, numpy.prod(shape)) - a = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(shape) + a = generate_random_numpy_array(shape, dtype) ia = dpnp.array(a) result = dpnp.linalg.cond(ia, p=p) @@ -345,7 +324,7 @@ def test_bool(self, p): "p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"] ) def test_nan(self, p): - a = numpy.array(numpy.random.uniform(-5, 5, 16)).reshape(2, 2, 2, 2) + a = generate_random_numpy_array((2, 2, 2, 2)) a[0, 0] = 0 a[1, 1] = 0 ia = dpnp.array(a) @@ -524,7 +503,7 @@ def test_eigenvalues(self, func, shape, dtype, order): # non-symmetric for eig() and eigvals() is_hermitian = func in ("eigh, eigvalsh") a = generate_random_numpy_array( - shape, dtype, hermitian=is_hermitian, seed_value=81 + shape, dtype, hermitian=is_hermitian, low=-4, high=4 ) a_order = numpy.array(a, order=order) a_dp = dpnp.array(a, order=order) @@ -544,7 +523,7 @@ def test_eigenvalues(self, func, shape, dtype, order): w = getattr(numpy.linalg, func)(a_order) w_dp = getattr(dpnp.linalg, func)(a_dp) - assert_dtype_allclose(w_dp, w) + assert_dtype_allclose(w_dp, w, factor=24) # eigh() and eigvalsh() are tested in cupy tests @pytest.mark.parametrize("func", ["eig", "eigvals"]) @@ -1826,11 +1805,11 @@ def test_lstsq(self, a_shape, b_shape, dtype): for param_dp, param_np in zip(result, expected): assert_dtype_allclose(param_dp, param_np) - @pytest.mark.parametrize("a_dtype", get_all_dtypes()) + @pytest.mark.parametrize("a_dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("b_dtype", get_all_dtypes()) def test_lstsq_diff_type(self, a_dtype, b_dtype): - a_np = numpy.array([[1, 2], [3, -5]], dtype=a_dtype) - b_np = numpy.array([4, 1], dtype=b_dtype) + a_np = generate_random_numpy_array((2, 2), a_dtype) + b_np = generate_random_numpy_array(2, b_dtype) a_dp = dpnp.array(a_np) b_dp = dpnp.array(b_np) @@ -2145,14 +2124,14 @@ def test_0D(self, ord, axis): assert_dtype_allclose(result, expected) @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize( "ord", [None, -dpnp.inf, -2, -1, 0, 1, 2, 3.5, dpnp.inf] ) @pytest.mark.parametrize("axis", [0, None]) @pytest.mark.parametrize("keepdims", [True, False]) def test_1D(self, dtype, ord, axis, keepdims): - a = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + a = generate_random_numpy_array(10, dtype) ia = dpnp.array(a) result = dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) @@ -2160,24 +2139,7 @@ def test_1D(self, dtype, ord, axis, keepdims): assert_dtype_allclose(result, expected) @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "ord", [None, -dpnp.inf, -2, -1, 0, 1, 2, 3.5, dpnp.inf] - ) - @pytest.mark.parametrize("axis", [0, None]) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_1D_complex(self, dtype, ord, axis, keepdims): - x1 = numpy.random.uniform(-5, 5, 10) - x2 = numpy.random.uniform(-5, 5, 10) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - ia = dpnp.array(a) - - result = dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - expected = numpy.linalg.norm(a, ord=ord, axis=axis, keepdims=keepdims) - assert_dtype_allclose(result, expected) - - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize( "ord", [None, -dpnp.inf, -2, -1, 1, 2, 3, dpnp.inf, "fro", "nuc"] ) @@ -2202,34 +2164,7 @@ def test_2D(self, dtype, ord, axis, keepdims): assert_dtype_allclose(result, expected) @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "ord", [None, -dpnp.inf, -2, -1, 1, 2, 3, dpnp.inf, "fro", "nuc"] - ) - @pytest.mark.parametrize( - "axis", [0, 1, (1, 0), None], ids=["0", "1", "(1, 0)", "None"] - ) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_2D_complex(self, dtype, ord, axis, keepdims): - x1 = numpy.random.uniform(-5, 5, 15) - x2 = numpy.random.uniform(-5, 5, 15) - a = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(3, 5) - ia = dpnp.array(a) - if (axis in [-1, 0, 1] and ord in ["nuc", "fro"]) or ( - (isinstance(axis, tuple) or axis is None) and ord == 3 - ): - # Invalid norm order for vectors - with pytest.raises(ValueError): - dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - else: - result = dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - expected = numpy.linalg.norm( - a, ord=ord, axis=axis, keepdims=keepdims - ) - assert_dtype_allclose(result, expected) - - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "ord", [None, -dpnp.inf, -2, -1, 1, 2, 3, dpnp.inf, "fro", "nuc"] ) @@ -2259,39 +2194,6 @@ def test_ND(self, dtype, ord, axis, keepdims): ) assert_dtype_allclose(result, expected) - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "ord", [None, -dpnp.inf, -2, -1, 1, 2, 3, dpnp.inf, "fro", "nuc"] - ) - @pytest.mark.parametrize( - "axis", - [-1, 0, 1, (0, 1), (-1, -2), None], - ids=["-1", "0", "1", "(0, 1)", "(-1, -2)", "None"], - ) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_ND_complex(self, dtype, ord, axis, keepdims): - x1 = numpy.random.uniform(-5, 5, 120) - x2 = numpy.random.uniform(-5, 5, 120) - a = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(2, 3, 4, 5) - ia = dpnp.array(a) - if (axis in [-1, 0, 1] and ord in ["nuc", "fro"]) or ( - isinstance(axis, tuple) and ord == 3 - ): - # Invalid norm order for vectors - with pytest.raises(ValueError): - dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - elif axis is None and ord is not None: - # Improper number of dimensions to norm - with pytest.raises(ValueError): - dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - else: - result = dpnp.linalg.norm(ia, ord=ord, axis=axis, keepdims=keepdims) - expected = numpy.linalg.norm( - a, ord=ord, axis=axis, keepdims=keepdims - ) - assert_dtype_allclose(result, expected) - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize( @@ -2304,9 +2206,7 @@ def test_ND_complex(self, dtype, ord, axis, keepdims): ) @pytest.mark.parametrize("keepdims", [True, False]) def test_usm_ndarray(self, dtype, ord, axis, keepdims): - a = numpy.array(numpy.random.uniform(-5, 5, 120), dtype=dtype).reshape( - 2, 3, 4, 5 - ) + a = generate_random_numpy_array((2, 3, 4, 5), dtype) ia = dpt.asarray(a) if (axis in [-1, 0, 1] and ord in ["nuc", "fro"]) or ( isinstance(axis, tuple) and ord == 3 @@ -2381,7 +2281,7 @@ def test_strided_ND(self, axis, stride): ) @pytest.mark.parametrize("keepdims", [True, False]) def test_matrix_norm(self, ord, keepdims): - a = numpy.array(numpy.random.uniform(-5, 5, 15)).reshape(3, 5) + a = generate_random_numpy_array((3, 5)) ia = dpnp.array(a) result = dpnp.linalg.matrix_norm(ia, ord=ord, keepdims=keepdims) @@ -2407,7 +2307,7 @@ def test_vector_norm_0D(self, ord): @pytest.mark.parametrize("axis", [0, None]) @pytest.mark.parametrize("keepdims", [True, False]) def test_vector_norm_1D(self, ord, axis, keepdims): - a = numpy.array(numpy.random.uniform(-5, 5, 10)) + a = generate_random_numpy_array(10) ia = dpnp.array(a) result = dpnp.linalg.vector_norm( @@ -2474,8 +2374,6 @@ class TestQr: ) @pytest.mark.parametrize("mode", ["r", "raw", "complete", "reduced"]) def test_qr(self, dtype, shape, mode): - # Set seed_value=81 to prevent - # random generation of the input singular matrix a = generate_random_numpy_array(shape, dtype, seed_value=81) ia = dpnp.array(a) @@ -2604,13 +2502,8 @@ def test_solve(self, dtype): ], ) def test_solve_broadcast(self, a_shape, b_shape, dtype): - # Set seed_value=81 to prevent - # random generation of the input singular matrix - a_np = generate_random_numpy_array(a_shape, dtype, seed_value=81) - - # Set seed_value=76 to prevent - # random generation of the input singular matrix - b_np = generate_random_numpy_array(b_shape, dtype, seed_value=76) + a_np = generate_random_numpy_array(a_shape, dtype) + b_np = generate_random_numpy_array(b_shape, dtype) a_dp = dpnp.array(a_np) b_dp = dpnp.array(b_np) @@ -2638,8 +2531,8 @@ def test_solve_nrhs_greater_n(self, dtype): @pytest.mark.parametrize("a_dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize("b_dtype", get_all_dtypes(no_bool=True)) def test_solve_diff_type(self, a_dtype, b_dtype): - a_np = numpy.array([[1, 2], [3, -5]], dtype=a_dtype) - b_np = numpy.array([4, 1], dtype=b_dtype) + a_np = generate_random_numpy_array((2, 2), a_dtype) + b_np = generate_random_numpy_array(2, b_dtype) a_dp = dpnp.array(a_np) b_dp = dpnp.array(b_np) @@ -2842,13 +2735,13 @@ class TestSvd: def get_tol(self, dtype): tol = 1e-06 if dtype in (dpnp.float32, dpnp.complex64): - tol = 1e-04 + tol = 1e-03 elif not has_support_aspect64() and dtype in ( dpnp.int32, dpnp.int64, None, ): - tol = 1e-04 + tol = 1e-03 self._tol = tol def check_types_shapes( @@ -2934,11 +2827,7 @@ def test_svd(self, dtype, shape): "shape", [(2, 2), (16, 16)], ids=["(2, 2)", "(16, 16)"] ) def test_svd_hermitian(self, dtype, compute_vt, shape): - # Set seed_value=81 to prevent - # random generation of the input singular matrix - a = generate_random_numpy_array( - shape, dtype, hermitian=True, seed_value=81 - ) + a = generate_random_numpy_array(shape, dtype, hermitian=True) dp_a = dpnp.array(a) if compute_vt: @@ -3023,13 +2912,13 @@ class TestPinv: def get_tol(self, dtype): tol = 1e-06 if dtype in (dpnp.float32, dpnp.complex64): - tol = 1e-04 + tol = 1e-03 elif not has_support_aspect64() and dtype in ( dpnp.int32, dpnp.int64, None, ): - tol = 1e-04 + tol = 1e-03 self._tol = tol def check_types_shapes(self, dp_B, np_B): @@ -3055,8 +2944,6 @@ def check_types_shapes(self, dp_B, np_B): ], ) def test_pinv(self, dtype, shape): - # Set seed_value=81 to prevent - # random generation of the input singular matrix a = generate_random_numpy_array(shape, dtype, seed_value=81) a_dp = dpnp.array(a) @@ -3080,10 +2967,8 @@ def test_pinv(self, dtype, shape): "shape", [(2, 2), (16, 16)], ids=["(2, 2)", "(16, 16)"] ) def test_pinv_hermitian(self, dtype, shape): - # Set seed_value=81 to prevent - # random generation of the input singular matrix a = generate_random_numpy_array( - shape, dtype, hermitian=True, seed_value=81 + shape, dtype, hermitian=True, seed_value=64, low=-5, high=5 ) a_dp = dpnp.array(a) diff --git a/dpnp/tests/test_mathematical.py b/dpnp/tests/test_mathematical.py index 7f250afaba8..756367c4bcb 100644 --- a/dpnp/tests/test_mathematical.py +++ b/dpnp/tests/test_mathematical.py @@ -81,47 +81,24 @@ def test_angle_complex(self, dtype, deg): class TestConj: - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_conj(self, dtype): - a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype) - ia = dpnp.array(a) - - result = dpnp.conj(ia) - expected = numpy.conj(a) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_conj_complex(self, dtype): - x1 = numpy.random.uniform(-5, 5, 20) - x2 = numpy.random.uniform(-5, 5, 20) - a = numpy.array(x1 + 1j * x2, dtype=dtype) + a = generate_random_numpy_array(20, dtype) ia = dpnp.array(a) result = dpnp.conj(ia) expected = numpy.conj(a) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) - def test_conj_ndarray(self, dtype): - a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype) - ia = dpnp.array(a) - + # ndarray result = ia.conj() - assert result is ia + if not dpnp.issubdtype(dtype, dpnp.complexfloating): + assert result is ia assert_dtype_allclose(result, a.conj()) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_conj_complex_ndarray(self, dtype): - x1 = numpy.random.uniform(-5, 5, 20) - x2 = numpy.random.uniform(-5, 5, 20) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - ia = dpnp.array(a) - - assert_dtype_allclose(ia.conj(), a.conj()) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_conj_out(self, dtype): - a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype) + a = generate_random_numpy_array(20, dtype) ia = dpnp.array(a) expected = numpy.conj(a) @@ -2322,7 +2299,7 @@ def test_basic(self, dt): "dt", get_all_dtypes(no_none=True, no_complex=True) ) def test_rand(self, dt): - a = generate_random_numpy_array((10,), seed_value=42) * 100 + a = generate_random_numpy_array(10) * 100 a = a.astype(dtype=dt) ia = dpnp.array(a) @@ -2346,7 +2323,7 @@ def test_period(self, dt): "dt", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) ) def test_rand_period(self, dt): - a = generate_random_numpy_array((10,), seed_value=42) * 1000 + a = generate_random_numpy_array(10) * 1000 a = a.astype(dtype=dt) ia = dpnp.array(a) @@ -2650,56 +2627,29 @@ def test_signbit(data, dtype): class TestRealImag: - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_real_imag(self, dtype): - a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype) - ia = dpnp.array(a) - - result = dpnp.real(ia) - assert result is ia - expected = numpy.real(a) - assert expected is a - assert_dtype_allclose(result, expected) - - result = dpnp.imag(ia) - expected = numpy.imag(a) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_real_imag_complex(self, dtype): - x1 = numpy.random.uniform(-5, 5, 20) - x2 = numpy.random.uniform(-5, 5, 20) - a = numpy.array(x1 + 1j * x2, dtype=dtype) + a = generate_random_numpy_array(20, dtype) ia = dpnp.array(a) result = dpnp.real(ia) expected = numpy.real(a) + if not dpnp.issubdtype(dtype, dpnp.complexfloating): + assert result is ia + assert expected is a assert_dtype_allclose(result, expected) result = dpnp.imag(ia) expected = numpy.imag(a) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) - def test_real_imag_ndarray(self, dtype): - a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype) - ia = dpnp.array(a) - + # ndarray result = ia.real - assert result is ia + if not dpnp.issubdtype(dtype, dpnp.complexfloating): + assert result is ia assert_dtype_allclose(result, a.real) assert_dtype_allclose(ia.imag, a.imag) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_real_imag_complex_ndarray(self, dtype): - x1 = numpy.random.uniform(-5, 5, 20) - x2 = numpy.random.uniform(-5, 5, 20) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - ia = dpnp.array(a) - - assert_dtype_allclose(ia.real, a.real) - assert_dtype_allclose(ia.imag, a.imag) - class TestProjection: @pytest.mark.parametrize("dtype", get_complex_dtypes()) @@ -3730,30 +3680,6 @@ def test_matmul_bool(self, shape1, shape2): expected = numpy.matmul(a1, a2) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((2, 4), (4, 3)), - ((4, 2, 3), (4, 3, 5)), - ((6, 7, 4, 3), (6, 7, 3, 5)), - ], - ids=[ - "((2, 4), (4, 3))", - "((4, 2, 3), (4, 3, 5))", - "((6, 7, 4, 3), (6, 7, 3, 5))", - ], - ) - def test_matmul_dtype(self, dtype, shape1, shape2): - a1 = numpy.arange(numpy.prod(shape1)).reshape(shape1) - a2 = numpy.arange(numpy.prod(shape2)).reshape(shape2) - b1 = dpnp.asarray(a1) - b2 = dpnp.asarray(a2) - - result = dpnp.matmul(b1, b2, dtype=dtype) - expected = numpy.matmul(a1, a2, dtype=dtype) - assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( "axes", @@ -3764,12 +3690,8 @@ def test_matmul_dtype(self, dtype, shape1, shape2): ], ) def test_matmul_axes_ND_ND(self, dtype, axes): - a = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(2, 5, 3, 4) - b = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(4, 2, 5, 3) + a = generate_random_numpy_array((2, 5, 3, 4), dtype) + b = generate_random_numpy_array((4, 2, 5, 3), dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -3837,12 +3759,8 @@ def test_matmul_axes_1D_1D(self): ], ) def test_matmul_axes_out(self, dtype, axes, out_shape): - a = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(2, 5, 3, 4) - b = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(4, 2, 5, 3) + a = generate_random_numpy_array((2, 5, 3, 4), dtype) + b = generate_random_numpy_array((4, 2, 5, 3), dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -3875,9 +3793,9 @@ def test_matmul_axes_out_1D(self, axes, b_shape, out_shape): expected = numpy.matmul(a, b, axes=axes, out=out_np) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype1", get_all_dtypes(no_bool=True)) + @pytest.mark.parametrize("in_dt", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( - "dtype2", get_all_dtypes(no_bool=True, no_none=True) + "out_dt", get_all_dtypes(no_bool=True, no_none=True) ) @pytest.mark.parametrize( "shape1, shape2", @@ -3892,19 +3810,19 @@ def test_matmul_axes_out_1D(self, axes, b_shape, out_shape): "((6, 7, 4, 3), (6, 7, 3, 5))", ], ) - def test_matmul_dtype_matrix_inout(self, dtype1, dtype2, shape1, shape2): - a1 = numpy.arange(numpy.prod(shape1), dtype=dtype1).reshape(shape1) - a2 = numpy.arange(numpy.prod(shape2), dtype=dtype1).reshape(shape2) + def test_matmul_dtype_matrix_inout(self, in_dt, out_dt, shape1, shape2): + a1 = generate_random_numpy_array(shape1, in_dt) + a2 = generate_random_numpy_array(shape2, in_dt) b1 = dpnp.asarray(a1) b2 = dpnp.asarray(a2) - if dpnp.can_cast(dpnp.result_type(b1, b2), dtype2, casting="same_kind"): - result = dpnp.matmul(b1, b2, dtype=dtype2) - expected = numpy.matmul(a1, a2, dtype=dtype2) + if dpnp.can_cast(dpnp.result_type(b1, b2), out_dt, casting="same_kind"): + result = dpnp.matmul(b1, b2, dtype=out_dt) + expected = numpy.matmul(a1, a2, dtype=out_dt) assert_dtype_allclose(result, expected) else: with pytest.raises(TypeError): - dpnp.matmul(b1, b2, dtype=dtype2) + dpnp.matmul(b1, b2, dtype=out_dt) @pytest.mark.parametrize("dtype1", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize("dtype2", get_all_dtypes(no_bool=True)) @@ -3922,8 +3840,8 @@ def test_matmul_dtype_matrix_inout(self, dtype1, dtype2, shape1, shape2): ], ) def test_matmul_dtype_matrix_inputs(self, dtype1, dtype2, shape1, shape2): - a1 = numpy.arange(numpy.prod(shape1), dtype=dtype1).reshape(shape1) - a2 = numpy.arange(numpy.prod(shape2), dtype=dtype2).reshape(shape2) + a1 = generate_random_numpy_array(shape1, dtype1) + a2 = generate_random_numpy_array(shape2, dtype2) b1 = dpnp.asarray(a1) b2 = dpnp.asarray(a2) @@ -4244,10 +4162,8 @@ def test_matmul_out_0D(self, out_shape): ], ) def test_matmul_large(self, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array(numpy.random.uniform(-5, 5, size1)).reshape(shape1) - b = numpy.array(numpy.random.uniform(-5, 5, size2)).reshape(shape2) + a = generate_random_numpy_array(shape1) + b = generate_random_numpy_array(shape2) a_dp = dpnp.asarray(a) b_dp = dpnp.asarray(b) @@ -4275,14 +4191,13 @@ def test_linalg_matmul(self): ids=["gemm", "gemm_batch"], ) def test_matmul_with_offsets(self, sh1, sh2): - size1, size2 = numpy.prod(sh1, dtype=int), numpy.prod(sh2, dtype=int) - a = numpy.random.randint(-5, 5, size1).reshape(sh1).astype("f8") - b = numpy.random.randint(-5, 5, size2).reshape(sh2).astype("f8") + a = generate_random_numpy_array(sh1) + b = generate_random_numpy_array(sh2) ia, ib = dpnp.array(a), dpnp.array(b) result = ia[1] @ ib[1] expected = a[1] @ b[1] - assert_array_equal(result, expected) + assert_dtype_allclose(result, expected) class TestMatmulInplace: diff --git a/dpnp/tests/test_product.py b/dpnp/tests/test_product.py index 9778240c91c..95c643a6494 100644 --- a/dpnp/tests/test_product.py +++ b/dpnp/tests/test_product.py @@ -6,7 +6,12 @@ import dpnp -from .helper import assert_dtype_allclose, get_all_dtypes, get_complex_dtypes +from .helper import ( + assert_dtype_allclose, + generate_random_numpy_array, + get_all_dtypes, + get_complex_dtypes, +) from .third_party.cupy import testing @@ -52,9 +57,7 @@ def test_3x3(self, x1, x2, axisa, axisb, axisc, axis): assert_dtype_allclose(result, expected) @pytest.mark.filterwarnings("ignore::DeprecationWarning") - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( "shape1, shape2, axis_a, axis_b, axis_c", [ @@ -66,38 +69,8 @@ def test_3x3(self, x1, x2, axisa, axisb, axisc, axis): ], ) def test_basic(self, dtype, shape1, shape2, axis_a, axis_b, axis_c): - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape1)), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape2)), dtype=dtype - ).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.cross(ia, ib, axis_a, axis_b, axis_c) - expected = numpy.cross(a, b, axis_a, axis_b, axis_c) - assert_dtype_allclose(result, expected) - - @pytest.mark.filterwarnings("ignore::DeprecationWarning") - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2, axis_a, axis_b, axis_c", - [ - ((4, 2, 3, 5), (2, 4, 3, 5), 1, 0, -2), - ((2, 2, 4, 5), (2, 4, 3, 5), 1, 2, -1), - ((2, 3, 4, 5), (2, 4, 2, 5), 1, 2, -1), - ((2, 3, 4, 5), (2, 4, 3, 5), 1, 2, -1), - ((2, 3, 4, 5), (2, 4, 3, 5), -3, -2, 0), - ], - ) - def test_complex(self, dtype, shape1, shape2, axis_a, axis_b, axis_c): - x11 = numpy.random.uniform(-5, 5, numpy.prod(shape1)) - x12 = numpy.random.uniform(-5, 5, numpy.prod(shape1)) - x21 = numpy.random.uniform(-5, 5, numpy.prod(shape2)) - x22 = numpy.random.uniform(-5, 5, numpy.prod(shape2)) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -115,37 +88,31 @@ def test_complex(self, dtype, shape1, shape2, axis_a, axis_b, axis_c): ], ) def test_axis(self, dtype, shape1, shape2, axis): - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape1)), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape2)), dtype=dtype - ).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) result = dpnp.cross(ia, ib, axis=axis) expected = numpy.cross(a, b, axis=axis) - assert_dtype_allclose(result, expected) + assert_dtype_allclose(result, expected, factor=24) @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): if dtype1 == dpnp.bool and dtype2 == dpnp.bool: pytest.skip("boolean input arrays is not supported.") - a = numpy.array(numpy.random.uniform(-5, 5, 3), dtype=dtype1) - b = numpy.array(numpy.random.uniform(-5, 5, 3), dtype=dtype2) + a = generate_random_numpy_array(3, dtype1) + b = generate_random_numpy_array(3, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) result = dpnp.cross(ia, ib) expected = numpy.cross(a, b) - assert_dtype_allclose(result, expected) + assert_dtype_allclose(result, expected, factor=24) @pytest.mark.filterwarnings("ignore::DeprecationWarning") - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( "shape1, shape2, axis_a, axis_b, axis_c", [ @@ -157,18 +124,14 @@ def test_input_dtype_matrix(self, dtype1, dtype2): ], ) def test_broadcast(self, dtype, shape1, shape2, axis_a, axis_b, axis_c): - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape1)), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape2)), dtype=dtype - ).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) result = dpnp.cross(ia, ib, axis_a, axis_b, axis_c) expected = numpy.cross(a, b, axis_a, axis_b, axis_c) - assert_dtype_allclose(result, expected) + assert_dtype_allclose(result, expected, factor=24) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize("stride", [3, -3]) @@ -225,7 +188,7 @@ class TestDot: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_ones(self, dtype): n = 10**5 a = numpy.ones(n, dtype=dtype) @@ -250,10 +213,10 @@ def test_arange(self, dtype): expected = numpy.dot(a, b) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_scalar(self, dtype): a = 2 - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + b = generate_random_numpy_array(10, dtype) ib = dpnp.array(b) result = dpnp.dot(a, ib) @@ -264,7 +227,7 @@ def test_scalar(self, dtype): expected = numpy.dot(b, a) _assert_selective_dtype_allclose(result, expected, dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shape1, shape2", [ @@ -293,14 +256,8 @@ def test_scalar(self, dtype): ], ) def test_basic(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -308,86 +265,7 @@ def test_basic(self, dtype, shape1, shape2): expected = numpy.dot(a, b) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((), (10,)), - ((10,), ()), - ((), ()), - ((10,), (10,)), - ((4, 3), (3, 2)), - ((4, 3), (3,)), - ((5, 4, 3), (3,)), - ((4,), (4, 2)), - ((5, 3, 4), (6, 4, 2)), - ], - ids=[ - "0d_1d", - "1d_0d", - "0d_0d", - "1d_1d", - "2d_2d", - "2d_1d", - "3d_1d", - "1d_2d", - "3d_3d", - ], - ) - def test_complex(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x11 = numpy.random.uniform(-5, 5, size1) - x12 = numpy.random.uniform(-5, 5, size1) - x21 = numpy.random.uniform(-5, 5, size2) - x22 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.dot(ia, ib) - expected = numpy.dot(a, b) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_all_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((), (10,)), - ((10,), ()), - ((), ()), - ((10,), (10,)), - ((4, 3), (3, 2)), - ((4, 3), (3,)), - ((5, 4, 3), (3,)), - ((4,), (4, 2)), - ((5, 3, 4), (6, 4, 2)), - ], - ids=[ - "0d_1d", - "1d_0d", - "0d_0d", - "1d_1d", - "2d_2d", - "2d_1d", - "3d_1d", - "1d_2d", - "3d_3d", - ], - ) - def test_ndarray(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - + # ndarray result = ia.dot(ib) expected = a.dot(b) assert_dtype_allclose(result, expected) @@ -407,10 +285,10 @@ def test_strided(self, dtype, stride): @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_out_scalar(self, dtype): a = 2 - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + b = generate_random_numpy_array(10, dtype) ib = dpnp.array(b) - dp_out = dpnp.empty((10,), dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype) result = dpnp.dot(a, ib, out=dp_out) expected = numpy.dot(a, b) @@ -446,14 +324,8 @@ def test_out_scalar(self, dtype): ], ) def test_out(self, dtype, shape1, shape2, out_shape): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -467,8 +339,8 @@ def test_out(self, dtype, shape1, shape2, out_shape): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - a = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype1) - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype2) + a = generate_random_numpy_array(10, dtype1) + b = generate_random_numpy_array(10, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -562,10 +434,10 @@ class TestInner: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_scalar(self, dtype): a = 2 - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + b = generate_random_numpy_array(10, dtype) ib = dpnp.array(b) result = dpnp.inner(a, ib) @@ -576,7 +448,7 @@ def test_scalar(self, dtype): expected = numpy.inner(b, a) _assert_selective_dtype_allclose(result, expected, dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shape1, shape2", [ @@ -588,41 +460,8 @@ def test_scalar(self, dtype): ], ) def test_basic(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.inner(ia, ib) - expected = numpy.inner(a, b) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((5,), (5,)), - ((3, 5), (3, 5)), - ((2, 4, 3, 5), (2, 4, 3, 5)), - ((), (3, 4)), - ((5,), ()), - ], - ) - def test_complex(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x11 = numpy.random.uniform(-5, 5, size1) - x12 = numpy.random.uniform(-5, 5, size1) - x21 = numpy.random.uniform(-5, 5, size2) - x22 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -633,8 +472,8 @@ def test_complex(self, dtype, shape1, shape2): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - a = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype1) - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype2) + a = generate_random_numpy_array(10, dtype1) + b = generate_random_numpy_array(10, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -663,10 +502,10 @@ def test_error(self): class TestKron: - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_scalar(self, dtype): a = 2 - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + b = generate_random_numpy_array(10, dtype) ib = dpnp.array(b) result = dpnp.kron(a, ib) @@ -677,7 +516,7 @@ def test_scalar(self, dtype): expected = numpy.kron(b, a) _assert_selective_dtype_allclose(result, expected, dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shape1, shape2", [ @@ -692,44 +531,8 @@ def test_scalar(self, dtype): ], ) def test_basic(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.kron(ia, ib) - expected = numpy.kron(a, b) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((5,), (5,)), - ((3, 5), (4, 6)), - ((2, 4, 3, 5), (3, 5, 6, 2)), - ((4, 3, 5), (3, 5, 6, 2)), - ((2, 4, 3, 5), (3, 5, 6)), - ((2, 4, 3, 5), (3,)), - ((), (3, 4)), - ((5,), ()), - ], - ) - def test_complex(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x11 = numpy.random.uniform(-5, 5, size1) - x12 = numpy.random.uniform(-5, 5, size1) - x21 = numpy.random.uniform(-5, 5, size2) - x22 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -740,8 +543,8 @@ def test_complex(self, dtype, shape1, shape2): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - a = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype1) - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype2) + a = generate_random_numpy_array(10, dtype1) + b = generate_random_numpy_array(10, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -794,7 +597,7 @@ class TestMultiDot: def setup_method(self): numpy.random.seed(70) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shapes", [ @@ -828,55 +631,7 @@ def test_basic(self, shapes, dtype): numpy_array_list = [] dpnp_array_list = [] for shape in shapes: - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape)), dtype=dtype - ).reshape(shape) - ia = dpnp.array(a) - - numpy_array_list.append(a) - dpnp_array_list.append(ia) - - result = dpnp.linalg.multi_dot(dpnp_array_list) - expected = numpy.linalg.multi_dot(numpy_array_list) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shapes", - [ - ((4, 5), (5, 4)), - ((4,), (4, 6), (6, 8)), - ((4, 8), (8, 6), (6,)), - ((6,), (6, 8), (8,)), - ((2, 10), (10, 5), (5, 8)), - ((8, 5), (5, 10), (10, 2)), - ((4, 6), (6, 9), (9, 7), (7, 8)), - ((6,), (6, 10), (10, 7), (7, 8)), - ((4, 6), (6, 10), (10, 7), (7,)), - ((6,), (6, 10), (10, 7), (7,)), - ((4, 6), (6, 9), (9, 7), (7, 8), (8, 3)), - ], - ids=[ - "two_arrays", - "three_arrays_1st_1D", - "three_arrays_last_1D", - "three_arrays_1st_last_1D", - "three_arrays_cost1", - "three_arrays_cost2", - "four_arrays", - "four_arrays_1st_1D", - "four_arrays_last_1D", - "four_arrays_1st_last_1D", - "five_arrays", - ], - ) - def test_complex(self, shapes, dtype): - numpy_array_list = [] - dpnp_array_list = [] - for shape in shapes: - x1 = numpy.random.uniform(-5, 5, numpy.prod(shape)) - x2 = numpy.random.uniform(-5, 5, numpy.prod(shape)) - a = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(shape) + a = generate_random_numpy_array(shape, dtype) ia = dpnp.array(a) numpy_array_list.append(a) @@ -884,9 +639,9 @@ def test_complex(self, shapes, dtype): result = dpnp.linalg.multi_dot(dpnp_array_list) expected = numpy.linalg.multi_dot(numpy_array_list) - assert_dtype_allclose(result, expected) + assert_dtype_allclose(result, expected, factor=24) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shapes", [ @@ -920,9 +675,7 @@ def test_out(self, shapes, dtype): numpy_array_list = [] dpnp_array_list = [] for shape in shapes[:-1]: - a = numpy.array( - numpy.random.uniform(-5, 5, numpy.prod(shape)), dtype=dtype - ).reshape(shape) + a = generate_random_numpy_array(shape, dtype) ia = dpnp.array(a) numpy_array_list.append(a) @@ -932,7 +685,7 @@ def test_out(self, shapes, dtype): result = dpnp.linalg.multi_dot(dpnp_array_list, out=dp_out) assert result is dp_out expected = numpy.linalg.multi_dot(numpy_array_list) - assert_dtype_allclose(result, expected) + assert_dtype_allclose(result, expected, factor=24) @pytest.mark.parametrize( "stride", @@ -1001,10 +754,10 @@ class TestTensordot: def setup_method(self): numpy.random.seed(87) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_scalar(self, dtype): a = 2 - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype) + b = generate_random_numpy_array(10, dtype) ib = dpnp.array(b) result = dpnp.tensordot(a, ib, axes=0) @@ -1015,31 +768,11 @@ def test_scalar(self, dtype): expected = numpy.tensordot(b, a, axes=0) _assert_selective_dtype_allclose(result, expected, dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("axes", [0, 1, 2]) def test_basic(self, dtype, axes): - a = numpy.array(numpy.random.uniform(-10, 10, 64), dtype=dtype).reshape( - 4, 4, 4 - ) - b = numpy.array(numpy.random.uniform(-10, 10, 64), dtype=dtype).reshape( - 4, 4, 4 - ) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.tensordot(ia, ib, axes=axes) - expected = numpy.tensordot(a, b, axes=axes) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize("axes", [0, 1, 2]) - def test_complex(self, dtype, axes): - x11 = numpy.random.uniform(-10, 10, 64) - x12 = numpy.random.uniform(-10, 10, 64) - x21 = numpy.random.uniform(-10, 10, 64) - x22 = numpy.random.uniform(-10, 10, 64) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(4, 4, 4) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(4, 4, 4) + a = generate_random_numpy_array((4, 4, 4), dtype) + b = generate_random_numpy_array((4, 4, 4), dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1059,12 +792,8 @@ def test_complex(self, dtype, axes): ], ) def test_axes(self, dtype, axes): - a = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(2, 5, 3, 4) - b = numpy.array( - numpy.random.uniform(-10, 10, 120), dtype=dtype - ).reshape(4, 2, 5, 3) + a = generate_random_numpy_array((2, 5, 3, 4), dtype) + b = generate_random_numpy_array((4, 2, 5, 3), dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1075,12 +804,8 @@ def test_axes(self, dtype, axes): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - a = numpy.array( - numpy.random.uniform(-10, 10, 60), dtype=dtype1 - ).reshape(3, 4, 5) - b = numpy.array( - numpy.random.uniform(-10, 10, 40), dtype=dtype2 - ).reshape(4, 5, 2) + a = generate_random_numpy_array((3, 4, 5), dtype1) + b = generate_random_numpy_array((4, 5, 2), dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1112,8 +837,8 @@ def test_strided(self, stride): [([0, 1]), ([0, 1], [1, 2]), ([-2, -3], [3, 2])], ) def test_linalg(self, axes): - a = numpy.array(numpy.random.uniform(-10, 10, 120)).reshape(2, 5, 3, 4) - b = numpy.array(numpy.random.uniform(-10, 10, 120)).reshape(4, 2, 5, 3) + a = generate_random_numpy_array((2, 5, 3, 4)) + b = generate_random_numpy_array((4, 2, 5, 3)) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1163,7 +888,7 @@ class TestVdot: def setup_method(self): numpy.random.seed(42) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_scalar(self, dtype): a = numpy.array([3.5], dtype=dtype) ia = dpnp.array(a) @@ -1177,7 +902,7 @@ def test_scalar(self, dtype): expected = numpy.vdot(b, a) _assert_selective_dtype_allclose(result, expected, dtype) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "shape1, shape2", [ @@ -1200,52 +925,8 @@ def test_scalar(self, dtype): ], ) def test_basic(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array( - numpy.random.uniform(-5, 5, size1), dtype=dtype - ).reshape(shape1) - b = numpy.array( - numpy.random.uniform(-5, 5, size2), dtype=dtype - ).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.vdot(ia, ib) - expected = numpy.vdot(a, b) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((), ()), - ((10,), (10,)), - ((4, 3), (3, 4)), - ((4, 3), (12,)), - ((5, 4, 3), (60,)), - ((8,), (4, 2)), - ((5, 3, 4), (3, 4, 5)), - ], - ids=[ - "0d_0d", - "1d_1d", - "2d_2d", - "2d_1d", - "3d_1d", - "1d_2d", - "3d_3d", - ], - ) - def test_vdot_complex(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x11 = numpy.random.uniform(-5, 5, size1) - x12 = numpy.random.uniform(-5, 5, size1) - x21 = numpy.random.uniform(-5, 5, size2) - x22 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1268,8 +949,8 @@ def test_strided(self, dtype, stride): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - a = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype1) - b = numpy.array(numpy.random.uniform(-5, 5, 10), dtype=dtype2) + a = generate_random_numpy_array(10, dtype1) + b = generate_random_numpy_array(10, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1322,47 +1003,8 @@ def setup_method(self): ], ) def test_basic(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x1 = numpy.random.uniform(-5, 5, size1) - x2 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x1, dtype=dtype).reshape(shape1) - b = numpy.array(x2, dtype=dtype).reshape(shape2) - ia = dpnp.array(a) - ib = dpnp.array(b) - - result = dpnp.vecdot(ia, ib) - expected = numpy.vecdot(a, b) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - @pytest.mark.parametrize( - "shape1, shape2", - [ - ((4,), (4,)), # call_flag: dot - ((1, 1, 4), (1, 1, 4)), # call_flag: dot - ((3, 1), (3, 1)), - ((2, 0), (2, 0)), # zero-size inputs, 1D output - ((3, 0, 4), (3, 0, 4)), # zero-size output - ((3, 4), (3, 4)), - ((1, 4), (3, 4)), - ((4,), (3, 4)), - ((3, 4), (1, 4)), - ((3, 4), (4,)), - ((1, 4, 5), (3, 1, 5)), - ((1, 1, 4, 5), (3, 1, 5)), - ((1, 4, 5), (1, 3, 1, 5)), - ], - ) - def test_complex(self, dtype, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - x11 = numpy.random.uniform(-5, 5, size1) - x12 = numpy.random.uniform(-5, 5, size1) - x21 = numpy.random.uniform(-5, 5, size2) - x22 = numpy.random.uniform(-5, 5, size2) - a = numpy.array(x11 + 1j * x12, dtype=dtype).reshape(shape1) - b = numpy.array(x21 + 1j * x22, dtype=dtype).reshape(shape2) + a = generate_random_numpy_array(shape1, dtype) + b = generate_random_numpy_array(shape2, dtype) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1376,10 +1018,8 @@ def test_complex(self, dtype, shape1, shape2): [((4,), (4, 4, 4)), ((3, 4, 5), (3, 4, 5))], ) def test_axis1(self, axis, shape1, shape2): - size1 = numpy.prod(shape1, dtype=int) - size2 = numpy.prod(shape2, dtype=int) - a = numpy.array(numpy.random.uniform(-5, 5, size1)).reshape(shape1) - b = numpy.array(numpy.random.uniform(-5, 5, size2)).reshape(shape2) + a = generate_random_numpy_array(shape1) + b = generate_random_numpy_array(shape2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1408,7 +1048,7 @@ def test_axis2(self): ], ) def test_axes(self, axes): - a = numpy.array(numpy.random.uniform(-10, 10, 125)).reshape(5, 5, 5) + a = generate_random_numpy_array((5, 5, 5)) ia = dpnp.array(a) result = dpnp.vecdot(ia, ia, axes=axes) @@ -1455,10 +1095,8 @@ def test_strided(self, stride): @pytest.mark.parametrize("dtype1", get_all_dtypes()) @pytest.mark.parametrize("dtype2", get_all_dtypes()) def test_input_dtype_matrix(self, dtype1, dtype2): - x1 = numpy.random.uniform(-5, 5, 10) - x2 = numpy.random.uniform(-5, 5, 10) - a = numpy.array(x1, dtype=dtype1).reshape(2, 5) - b = numpy.array(x2, dtype=dtype2).reshape(2, 5) + a = generate_random_numpy_array(10, dtype1) + b = generate_random_numpy_array(10, dtype2) ia = dpnp.array(a) ib = dpnp.array(b) @@ -1587,12 +1225,8 @@ def test_out_0D(self, out_shape): @pytest.mark.parametrize("axis", [0, 1, 2, -1, -2, -3]) def test_linalg(self, axis): - x11 = numpy.random.uniform(-5, 5, 4) - x12 = numpy.random.uniform(-5, 5, 4) - x21 = numpy.random.uniform(-5, 5, 64) - x22 = numpy.random.uniform(-5, 5, 64) - a = numpy.array(x11 + 1j * x12, dtype=numpy.complex64) - b = numpy.array(x21 + 1j * x22, dtype=numpy.complex64).reshape(4, 4, 4) + a = generate_random_numpy_array(4) + b = generate_random_numpy_array((4, 4, 4)) ia = dpnp.array(a) ib = dpnp.array(b) diff --git a/dpnp/tests/test_sort.py b/dpnp/tests/test_sort.py index 941910648f4..0a4fdfec528 100644 --- a/dpnp/tests/test_sort.py +++ b/dpnp/tests/test_sort.py @@ -7,6 +7,7 @@ from .helper import ( assert_dtype_allclose, + generate_random_numpy_array, get_all_dtypes, get_complex_dtypes, get_float_dtypes, @@ -16,57 +17,35 @@ class TestArgsort: @pytest.mark.parametrize("kind", [None, "stable", "mergesort", "radixsort"]) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_basic(self, kind, dtype): - a = numpy.random.uniform(-5, 5, 10) - a = numpy.array(a, dtype=dtype) - ia = dpnp.array(a) - - result = dpnp.argsort(ia, kind=kind) - expected = numpy.argsort(a, kind="stable") - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("kind", [None, "stable", "mergesort", "radixsort"]) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_complex(self, kind, dtype): - a = numpy.random.uniform(-5, 5, 10) - b = numpy.random.uniform(-5, 5, 10) - a = numpy.array(a + b * 1j, dtype=dtype) + a = generate_random_numpy_array(10, dtype) ia = dpnp.array(a) - if kind == "radixsort": + if dpnp.issubdtype(dtype, dpnp.complexfloating) and kind == "radixsort": assert_raises(ValueError, dpnp.argsort, ia, kind=kind) else: result = dpnp.argsort(ia, kind=kind) - expected = numpy.argsort(a) + expected = numpy.argsort(a, kind="stable") assert_dtype_allclose(result, expected) @pytest.mark.parametrize("axis", [None, -2, -1, 0, 1, 2]) def test_axis(self, axis): - a = numpy.random.uniform(-10, 10, 36) - a = numpy.array(a).reshape(3, 4, 3) + a = generate_random_numpy_array((3, 4, 3)) ia = dpnp.array(a) result = dpnp.argsort(ia, axis=axis) expected = numpy.argsort(a, axis=axis) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("axis", [None, -2, -1, 0, 1]) def test_ndarray(self, dtype, axis): - if dtype and issubclass(dtype, numpy.integer): - a = numpy.random.choice( - numpy.arange(-10, 10), replace=False, size=12 - ) - else: - a = numpy.random.uniform(-10, 10, 12) - a = numpy.array(a, dtype=dtype).reshape(6, 2) + a = generate_random_numpy_array((6, 2), dtype) ia = dpnp.array(a) result = ia.argsort(axis=axis) - expected = a.argsort(axis=axis) + expected = a.argsort(axis=axis, kind="stable") assert_dtype_allclose(result, expected) # this test validates that all different options of kind in dpnp are stable @@ -138,9 +117,9 @@ def test_n_elements(self, n, side): ia = dpnp.array(a) v = numpy.array([0, 1, 2]) - dp_v = dpnp.array(v) + iv = dpnp.array(v) - result = ia.searchsorted(dp_v, side=side) + result = ia.searchsorted(iv, side=side) expected = a.searchsorted(v, side=side) assert_equal(result, expected) @@ -150,9 +129,9 @@ def test_smart_resetting(self, side): ia = dpnp.array(a) v = numpy.array([6, 5, 4]) - dp_v = dpnp.array(v) + iv = dpnp.array(v) - result = ia.searchsorted(dp_v, side=side) + result = ia.searchsorted(iv, side=side) expected = a.searchsorted(v, side=side) assert_equal(result, expected) @@ -275,27 +254,12 @@ def test_v_scalar(self): class TestSort: @pytest.mark.parametrize("kind", [None, "stable", "mergesort", "radixsort"]) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_basic(self, kind, dtype): - a = numpy.random.uniform(-5, 5, 10) - a = numpy.array(a, dtype=dtype) + a = generate_random_numpy_array(10, dtype) ia = dpnp.array(a) - result = dpnp.sort(ia, kind=kind) - expected = numpy.sort(a) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("kind", [None, "stable", "mergesort", "radixsort"]) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_complex(self, kind, dtype): - a = numpy.random.uniform(-5, 5, 10) - b = numpy.random.uniform(-5, 5, 10) - a = numpy.array(a + b * 1j, dtype=dtype) - ia = dpnp.array(a) - - if kind == "radixsort": + if dpnp.issubdtype(dtype, dpnp.complexfloating) and kind == "radixsort": assert_raises(ValueError, dpnp.argsort, ia, kind=kind) else: result = dpnp.sort(ia, kind=kind) @@ -304,8 +268,7 @@ def test_complex(self, kind, dtype): @pytest.mark.parametrize("axis", [None, -2, -1, 0, 1, 2]) def test_axis(self, axis): - a = numpy.random.uniform(-10, 10, 36) - a = numpy.array(a).reshape(3, 4, 3) + a = generate_random_numpy_array((3, 4, 3)) ia = dpnp.array(a) result = dpnp.sort(ia, axis=axis) @@ -315,8 +278,7 @@ def test_axis(self, axis): @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize("axis", [-2, -1, 0, 1]) def test_ndarray(self, dtype, axis): - a = numpy.random.uniform(-10, 10, 12) - a = numpy.array(a, dtype=dtype).reshape(6, 2) + a = generate_random_numpy_array((6, 2), dtype) ia = dpnp.array(a) ia.sort(axis=axis) diff --git a/dpnp/tests/test_statistics.py b/dpnp/tests/test_statistics.py index 475a359640a..58c2bda2403 100644 --- a/dpnp/tests/test_statistics.py +++ b/dpnp/tests/test_statistics.py @@ -11,6 +11,7 @@ from .helper import ( assert_dtype_allclose, + generate_random_numpy_array, get_all_dtypes, get_complex_dtypes, get_float_complex_dtypes, @@ -24,8 +25,8 @@ class TestAverage: @pytest.mark.parametrize("axis", [None, 0, 1]) @pytest.mark.parametrize("returned", [True, False]) def test_avg_no_wgt(self, dtype, axis, returned): - ia = dpnp.array([[1, 1, 2], [3, 4, 5]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) result = dpnp.average(ia, axis=axis, returned=returned) expected = numpy.average(a, axis=axis, returned=returned) @@ -39,10 +40,10 @@ def test_avg_no_wgt(self, dtype, axis, returned): @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)]) @pytest.mark.parametrize("returned", [True, False]) def test_avg(self, dtype, axis, returned): - ia = dpnp.array([[1, 1, 2], [3, 4, 5]], dtype=dtype) - iw = dpnp.array([[3, 1, 2], [3, 4, 2]], dtype=dtype) - a = dpnp.asnumpy(ia) - w = dpnp.asnumpy(iw) + a = generate_random_numpy_array((2, 3), dtype) + w = generate_random_numpy_array((2, 3), dtype, low=0, high=10) + ia = dpnp.array(a) + iw = dpnp.array(w) result = dpnp.average(ia, axis=axis, weights=iw, returned=returned) expected = numpy.average(a, axis=axis, weights=w, returned=returned) @@ -53,19 +54,6 @@ def test_avg(self, dtype, axis, returned): else: assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_avg_complex(self, dtype): - x1 = numpy.random.rand(10) - x2 = numpy.random.rand(10) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - w = numpy.array(x2 + 1j * x1, dtype=dtype) - ia = dpnp.array(a) - iw = dpnp.array(w) - - expected = numpy.average(a, weights=w) - result = dpnp.average(ia, weights=iw) - assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( "weight", [[[3, 1, 2], [3, 4, 2]], ((3, 1, 2), (3, 4, 2))], @@ -90,18 +78,18 @@ def test_avg_weight_1D(self): @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_avg_strided(self, dtype): - ia = dpnp.arange(20, dtype=dtype) - iw = dpnp.arange(-10, 10, dtype=dtype) - a = dpnp.asnumpy(ia) - w = dpnp.asnumpy(iw) + a = generate_random_numpy_array(20, dtype) + w = generate_random_numpy_array(20, dtype) + ia = dpnp.array(a) + iw = dpnp.array(w) result = dpnp.average(ia[::-1], weights=iw[::-1]) expected = numpy.average(a[::-1], weights=w[::-1]) - assert_allclose(result, expected) + assert_dtype_allclose(result, expected) result = dpnp.average(ia[::2], weights=iw[::2]) expected = numpy.average(a[::2], weights=w[::2]) - assert_allclose(result, expected) + assert_dtype_allclose(result, expected) def test_avg_error(self): a = dpnp.arange(5) @@ -217,18 +205,18 @@ def test_error(self, func): class TestMean: - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)]) @pytest.mark.parametrize("keepdims", [True, False]) def test_mean(self, dtype, axis, keepdims): - ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) result = dpnp.mean(ia, axis=axis, keepdims=keepdims) expected = numpy.mean(a, axis=axis, keepdims=keepdims) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "axis, out_shape", [(0, (3,)), (1, (2,)), ((0, 1), ())] ) @@ -243,17 +231,6 @@ def test_mean_out(self, dtype, axis, out_shape): assert result is out_dp assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_mean_complex(self, dtype): - x1 = numpy.random.rand(10) - x2 = numpy.random.rand(10) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - ia = dpnp.array(a) - - expected = numpy.mean(a) - result = dpnp.mean(ia) - assert_dtype_allclose(result, expected) - @pytest.mark.usefixtures( "suppress_invalid_numpy_warnings", "suppress_mean_empty_slice_numpy_warnings", @@ -286,22 +263,17 @@ class TestMedian: @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("size", [1, 2, 3, 4, 8, 9]) def test_basic(self, dtype, size): - if dtype == dpnp.bool: - a = numpy.arange(2, dtype=dtype) - a = numpy.repeat(a, size) - else: - a = numpy.array(numpy.random.uniform(-5, 5, size), dtype=dtype) + a = generate_random_numpy_array(size, dtype) ia = dpnp.array(a) expected = numpy.median(a) result = dpnp.median(ia) - assert_dtype_allclose(result, expected) @pytest.mark.parametrize("axis", [None, 0, (-1,), [0, 1], (0, -2, -1)]) @pytest.mark.parametrize("keepdims", [True, False]) def test_axis(self, axis, keepdims): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) ia = dpnp.array(a) expected = numpy.median(a, axis=axis, keepdims=keepdims) @@ -397,8 +369,8 @@ class TestVar: @pytest.mark.parametrize("keepdims", [True, False]) @pytest.mark.parametrize("ddof", [0, 0.5, 1, 1.5, 2]) def test_var(self, dtype, axis, keepdims, ddof): - ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) expected = numpy.var(a, axis=axis, keepdims=keepdims, ddof=ddof) result = dpnp.var(ia, axis=axis, keepdims=keepdims, ddof=ddof) @@ -415,8 +387,8 @@ def test_var(self, dtype, axis, keepdims, ddof): @pytest.mark.parametrize("axis", [None, 0, 1]) @pytest.mark.parametrize("ddof", [0, 1]) def test_var_out(self, dtype, axis, ddof): - ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) expected = numpy.var(a, axis=axis, ddof=ddof) if has_support_aspect64(): @@ -481,8 +453,8 @@ class TestStd: @pytest.mark.parametrize("keepdims", [True, False]) @pytest.mark.parametrize("ddof", [0, 0.5, 1, 1.5, 2]) def test_std(self, dtype, axis, keepdims, ddof): - ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) expected = numpy.std(a, axis=axis, keepdims=keepdims, ddof=ddof) result = dpnp.std(ia, axis=axis, keepdims=keepdims, ddof=ddof) @@ -498,8 +470,8 @@ def test_std(self, dtype, axis, keepdims, ddof): @pytest.mark.parametrize("axis", [0, 1]) @pytest.mark.parametrize("ddof", [0, 1]) def test_std_out(self, dtype, axis, ddof): - ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - a = dpnp.asnumpy(ia) + a = generate_random_numpy_array((2, 3), dtype) + ia = dpnp.array(a) expected = numpy.std(a, axis=axis, ddof=ddof) if has_support_aspect64(): @@ -560,7 +532,7 @@ class TestCorrcoef: "suppress_divide_invalid_numpy_warnings", "suppress_dof_numpy_warnings", ) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("rowvar", [True, False]) def test_corrcoef(self, dtype, rowvar): ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) diff --git a/dpnp/tests/test_sycl_queue.py b/dpnp/tests/test_sycl_queue.py index 41af6812133..9da87c3db86 100644 --- a/dpnp/tests/test_sycl_queue.py +++ b/dpnp/tests/test_sycl_queue.py @@ -1653,7 +1653,7 @@ def test_eigenvalue(func, shape, device): # Set seed_value=81 to prevent # random generation of the input singular matrix a = generate_random_numpy_array( - shape, dtype, hermitian=is_hermitian, seed_value=81 + shape, dtype, hermitian=is_hermitian, seed_value=81, low=-5, high=5 ) dp_a = dpnp.array(a, device=device)