diff --git a/dpnp/dpnp_algo/dpnp_algo.pyx b/dpnp/dpnp_algo/dpnp_algo.pyx index f12707ccc761..4737bcfd3c76 100644 --- a/dpnp/dpnp_algo/dpnp_algo.pyx +++ b/dpnp/dpnp_algo/dpnp_algo.pyx @@ -48,6 +48,8 @@ cimport dpnp.dpnp_utils as utils cimport numpy import numpy +import operator + __all__ = [ "dpnp_astype", diff --git a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx index 50b8bb840701..7b538118b939 100644 --- a/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx +++ b/dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx @@ -189,31 +189,81 @@ cpdef utils.dpnp_descriptor dpnp_identity(n, result_dtype): return result -# TODO this function should work through dpnp_arange_c -cpdef tuple dpnp_linspace(start, stop, num, endpoint, retstep, dtype, axis): - cdef shape_type_c obj_shape = utils._object_to_tuple(num) - cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None) +def dpnp_linspace(start, stop, num, dtype=None, device=None, usm_type=None, sycl_queue=None, endpoint=True, retstep=False, axis=0): + usm_type_alloc, sycl_queue_alloc = utils_py.get_usm_allocations([start, stop]) - if endpoint: - steps_count = num - 1 - else: - steps_count = num + # Get sycl_queue. + if sycl_queue is None and device is None: + sycl_queue = sycl_queue_alloc + sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device) - # if there are steps, then fill values - if steps_count > 0: - step = (dpnp.float64(stop) - start) / steps_count - for i in range(1, result.size): - result.get_pyobj()[i] = start + step * i + # Get temporary usm_type for getting dtype. + if usm_type is None: + _usm_type = "device" if usm_type_alloc is None else usm_type_alloc else: - step = dpnp.nan + _usm_type = usm_type + + # Get dtype. + if not hasattr(start, "dtype") and not dpnp.isscalar(start): + start = dpnp.asarray(start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized) + if not hasattr(stop, "dtype") and not dpnp.isscalar(stop): + stop = dpnp.asarray(stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized) + dt = numpy.result_type(start, stop, float(num)) + dt = utils_py.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device) + if dtype is None: + dtype = dt + + if dpnp.isscalar(start) and dpnp.isscalar(stop): + # Call linspace() function for scalars. + res = dpnp_container.linspace(start, + stop, + num, + dtype=dt, + usm_type=_usm_type, + sycl_queue=sycl_queue_normalized, + endpoint=endpoint) + else: + num = operator.index(num) + if num < 0: + raise ValueError("Number of points must be non-negative") + + # Get final usm_type and copy arrays if needed with current dtype, usm_type and sycl_queue. + # Do not need to copy usm_ndarray by usm_type if it is not explicitly stated. + if usm_type is None: + usm_type = _usm_type + if not hasattr(start, "usm_type"): + _start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized) + else: + _start = dpnp.asarray(start, dtype=dt, sycl_queue=sycl_queue_normalized) + if not hasattr(stop, "usm_type"): + _stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized) + else: + _stop = dpnp.asarray(stop, dtype=dt, sycl_queue=sycl_queue_normalized) + else: + _start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized) + _stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized) - # if result is not empty, then fiil first and last elements - if num > 0: - result.get_pyobj()[0] = start - if endpoint and result.size > 1: - result.get_pyobj()[result.size - 1] = stop + # FIXME: issue #1304. Mathematical operations with scalar don't follow data type. + _num = dpnp.asarray((num - 1) if endpoint else num, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized) + + step = (_stop - _start) / _num + + res = dpnp_container.arange(0, + stop=num, + step=1, + dtype=dt, + usm_type=usm_type, + sycl_queue=sycl_queue_normalized) + + res = res.reshape((-1,) + (1,) * step.ndim) + res = res * step + _start + + if endpoint and num > 1: + res[-1] = dpnp_container.full(step.shape, _stop) - return (result.get_pyobj(), step) + if numpy.issubdtype(dtype, dpnp.integer): + dpnp.floor(res, out=res) + return res.astype(dtype) cpdef utils.dpnp_descriptor dpnp_logspace(start, stop, num, endpoint, base, dtype, axis): diff --git a/dpnp/dpnp_container.py b/dpnp/dpnp_container.py index 2adb2b9b7f20..12d28074b8fb 100644 --- a/dpnp/dpnp_container.py +++ b/dpnp/dpnp_container.py @@ -47,6 +47,7 @@ "empty", "eye", "full", + "linspace", "ones" "tril", "triu", @@ -126,6 +127,33 @@ def empty(shape, return dpnp_array(array_obj.shape, buffer=array_obj, order=order) +def eye(N, + M=None, + /, + *, + k=0, + dtype=None, + order="C", + device=None, + usm_type="device", + sycl_queue=None): + """Validate input parameters before passing them into `dpctl.tensor` module""" + dpu.validate_usm_type(usm_type, allow_none=False) + sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device) + if order is None: + order = 'C' + + """Creates `dpnp_array` with ones on the `k`th diagonal.""" + array_obj = dpt.eye(N, + M, + k=k, + dtype=dtype, + order=order, + usm_type=usm_type, + sycl_queue=sycl_queue_normalized) + return dpnp_array(array_obj.shape, buffer=array_obj, order=order) + + def full(shape, fill_value, *, @@ -153,31 +181,29 @@ def full(shape, return dpnp_array(array_obj.shape, buffer=array_obj, order=order) -def eye(N, - M=None, - /, - *, - k=0, - dtype=None, - order="C", - device=None, - usm_type="device", - sycl_queue=None): +def linspace(start, + stop, + /, + num, + *, + dtype=None, + device=None, + usm_type="device", + sycl_queue=None, + endpoint=True): """Validate input parameters before passing them into `dpctl.tensor` module""" dpu.validate_usm_type(usm_type, allow_none=False) sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device) - if order is None: - order = 'C' - """Creates `dpnp_array` with ones on the `k`th diagonal.""" - array_obj = dpt.eye(N, - M, - k=k, - dtype=dtype, - order=order, - usm_type=usm_type, - sycl_queue=sycl_queue_normalized) - return dpnp_array(array_obj.shape, buffer=array_obj, order=order) + """Creates `dpnp_array` with evenly spaced numbers of specified interval.""" + array_obj = dpt.linspace(start, + stop, + num, + dtype=dtype, + usm_type=usm_type, + sycl_queue=sycl_queue_normalized, + endpoint=endpoint) + return dpnp_array(array_obj.shape, buffer=array_obj) def meshgrid(*xi, indexing="xy"): diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 8d892edce6dc..257fd660fbba 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -50,6 +50,7 @@ import dpnp.dpnp_container as dpnp_container import dpctl.tensor as dpt +import dpctl __all__ = [ @@ -879,7 +880,18 @@ def identity(n, dtype=None, *, like=None): return call_origin(numpy.identity, n, dtype=dtype, like=like) -def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): +def linspace(start, + stop, + /, + num, + *, + dtype=None, + device=None, + usm_type=None, + sycl_queue=None, + endpoint=True, + retstep=False, + axis=0): """ Return evenly spaced numbers over a specified interval. @@ -888,6 +900,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis Limitations ----------- Parameter ``axis`` is supported only with default value ``0``. + Parameter ``retstep`` is supported only with default value ``False``. + Otherwise the function will be executed sequentially on CPU. See Also -------- @@ -913,16 +927,19 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis """ - if not use_origin_backend(): - if axis != 0: - checker_throw_value_error("linspace", "axis", axis, 0) - - res = dpnp_linspace(start, stop, num, endpoint, retstep, dtype, axis) - - if retstep: - return res - else: - return res[0] + if retstep is not False: + pass + elif axis != 0: + pass + else: + return dpnp_linspace(start, + stop, + num, + dtype=dtype, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + endpoint=endpoint) return call_origin(numpy.linspace, start, stop, num, endpoint, retstep, dtype, axis) diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index 097f98c32506..d598ea2ca9fd 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -418,12 +418,10 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid3 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5 -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1 -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2 -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_one_num_no_endopoint_with_retstep +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_with_retstep +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_zero_num_no_endopoint_with_retstep tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1 diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index c517b5cf9dee..3dedcff4af04 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -594,19 +594,13 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid5 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4 tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5 - -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1 tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int - -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2 -tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list - +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1 +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_one_num_no_endopoint_with_retstep +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_with_retstep +tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_zero_num_no_endopoint_with_retstep tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_logspace_zero_num - tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_1_{axes=None, norm=None, s=(1, None), shape=(3, 4)}::test_fft2 tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_7_{axes=(), norm=None, s=None, shape=(3, 4)}::test_fft2 tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_7_{axes=(), norm=None, s=None, shape=(3, 4)}::test_ifft2 diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index 71e6a7b7d07d..7944ff210031 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -513,6 +513,51 @@ def test_dpctl_tensor_input(func, args): assert_array_equal(X, Y) +@pytest.mark.parametrize("start", + [0, -5, 10, -2.5, 9.7], + ids=['0', '-5', '10', '-2.5', '9.7']) +@pytest.mark.parametrize("stop", + [0, 10, -2, 20.5, 1000], + ids=['0', '10', '-2', '20.5', '1000']) +@pytest.mark.parametrize("num", + [5, numpy.array(10), dpnp.array(17), dpt.asarray(100)], + ids=['5', 'numpy.array(10)', 'dpnp.array(17)', 'dpt.asarray(100)']) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_float16=False)) +def test_linspace(start, stop, num, dtype): + func = lambda xp: xp.linspace(start, stop, num, dtype=dtype) + + if numpy.issubdtype(dtype, dpnp.integer): + assert_allclose(func(numpy), func(dpnp), rtol=1) + else: + assert_allclose(func(numpy), func(dpnp), atol=numpy.finfo(dtype).eps) + + +@pytest.mark.parametrize("start_dtype", + [numpy.float64, numpy.float32, numpy.int64, numpy.int32], + ids=['float64', 'float32', 'int64', 'int32']) +@pytest.mark.parametrize("stop_dtype", + [numpy.float64, numpy.float32, numpy.int64, numpy.int32], + ids=['float64', 'float32', 'int64', 'int32']) +def test_linspace_dtype(start_dtype, stop_dtype): + start = numpy.array([1, 2, 3], dtype=start_dtype) + stop = numpy.array([11, 7, -2], dtype=stop_dtype) + dpnp.linspace(start, stop, 10) + + +@pytest.mark.parametrize("start", + [dpnp.array(1), dpnp.array([2.6]), numpy.array([[-6.7, 3]]), [1, -4], (3, 5)]) +@pytest.mark.parametrize("stop", + [dpnp.array([-4]), dpnp.array([[2.6], [- 4]]), numpy.array(2), [[-4.6]], (3,)]) +def test_linspace_arrays(start, stop): + func = lambda xp: xp.linspace(start, stop, 10) + assert func(numpy).shape == func(dpnp).shape + + +def test_linspace_complex(): + func = lambda xp: xp.linspace(0, 3 + 2j, num=1000) + assert_allclose(func(numpy), func(dpnp)) + + @pytest.mark.parametrize("arrays", [[], [[1]], [[1, 2, 3], [4, 5, 6]], [[1, 2], [3, 4], [5, 6]]], ids=['[]', '[[1]]', '[[1, 2, 3], [4, 5, 6]]', '[[1, 2], [3, 4], [5, 6]]']) diff --git a/tests/test_special.py b/tests/test_special.py index da9938d75e9c..21810661687a 100644 --- a/tests/test_special.py +++ b/tests/test_special.py @@ -7,7 +7,7 @@ def test_erf(): a = numpy.linspace(2.0, 3.0, num=10) ia = dpnp.linspace(2.0, 3.0, num=10) - numpy.testing.assert_array_equal(a, ia) + numpy.testing.assert_allclose(a, ia) expected = numpy.empty_like(a) for idx, val in enumerate(a): diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 3b86f06ed2ee..2197dbe5414c 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -1,5 +1,6 @@ import pytest from .helper import get_all_dtypes +import sys import dpnp import dpctl @@ -90,6 +91,9 @@ def vvsort(val, vec, size, xp): pytest.param("eye", [4, 2], {}), + pytest.param("linspace", + [0, 4, 8], + {}), pytest.param("ones", [(2,2)], {}), @@ -134,13 +138,63 @@ def test_empty_like(device_x, device_y): @pytest.mark.parametrize( - "func, kwargs", + "func, args, kwargs", + [ + pytest.param("full_like", + ['x0'], + {'fill_value': 5}), + pytest.param("ones_like", + ['x0'], + {}), + pytest.param("zeros_like", + ['x0'], + {}), + pytest.param("tril", + ['x0.reshape((2,2))'], + {}), + pytest.param("triu", + ['x0.reshape((2,2))'], + {}), + pytest.param("linspace", + ['x0', '4', '4'], + {}), + pytest.param("linspace", + ['1', 'x0', '4'], + {}) + ]) +@pytest.mark.parametrize("device", + valid_devices, + ids=[device.filter_string for device in valid_devices]) +def test_array_creation_follow_device(func, args, kwargs, device): + x_orig = numpy.array([1, 2, 3, 4]) + numpy_args = [eval(val, {'x0' : x_orig}) for val in args] + y_orig = getattr(numpy, func)(*numpy_args, **kwargs) + + x = dpnp.array([1, 2, 3, 4], device=device) + dpnp_args = [eval(val, {'x0' : x}) for val in args] + + y = getattr(dpnp, func)(*dpnp_args, **kwargs) + numpy.testing.assert_allclose(y_orig, y) + assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) + + +@pytest.mark.parametrize( + "func, args, kwargs", [ pytest.param("full_like", + ['x0'], {'fill_value': 5}), pytest.param("ones_like", + ['x0'], {}), pytest.param("zeros_like", + ['x0'], + {}), + pytest.param("linspace", + ['x0', '4', '4'], + {}), + pytest.param("linspace", + ['1', 'x0', '4'], {}) ]) @pytest.mark.parametrize("device_x", @@ -149,34 +203,26 @@ def test_empty_like(device_x, device_y): @pytest.mark.parametrize("device_y", valid_devices, ids=[device.filter_string for device in valid_devices]) -def test_array_creation_like(func, kwargs, device_x, device_y): - x_orig = numpy.ndarray([1, 2, 3]) - y_orig = getattr(numpy, func)(x_orig, **kwargs) +def test_array_creation_cross_device(func, args, kwargs, device_x, device_y): + if func is 'linspace' and sys.platform.startswith('win'): + pytest.skip("CPU driver experiences an instability on Windows.") - x = dpnp.ndarray([1, 2, 3], device=device_x) + x_orig = numpy.array([1, 2, 3, 4]) + numpy_args = [eval(val, {'x0' : x_orig}) for val in args] + y_orig = getattr(numpy, func)(*numpy_args, **kwargs) - y = getattr(dpnp, func)(x, **kwargs) - numpy.testing.assert_array_equal(y_orig, y) - assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) + x = dpnp.array([1, 2, 3, 4], device=device_x) + dpnp_args = [eval(val, {'x0' : x}) for val in args] dpnp_kwargs = dict(kwargs) dpnp_kwargs['device'] = device_y + + y = getattr(dpnp, func)(*dpnp_args, **dpnp_kwargs) + numpy.testing.assert_allclose(y_orig, y) - y = getattr(dpnp, func)(x, **dpnp_kwargs) - numpy.testing.assert_array_equal(y_orig, y) assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue) -@pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"]) -@pytest.mark.parametrize("device", - valid_devices, - ids=[device.filter_string for device in valid_devices]) -def test_tril_triu(func, device): - x0 = dpnp.ones((3,3), device=device) - x = getattr(dpnp, func)(x0) - assert_sycl_queue_equal(x.sycl_queue, x0.sycl_queue) - - @pytest.mark.parametrize("device_x", valid_devices, ids=[device.filter_string for device in valid_devices]) diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index 605cbb4f3e48..9cbef1405198 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -76,6 +76,10 @@ def test_coerced_usm_types_divide(usm_type_x, usm_type_y): ['x0']), pytest.param("empty_like", ['x0']), + pytest.param("linspace", + ['x0[0:2]', '4', '4']), + pytest.param("linspace", + ['0', 'x0[3:5]', '4']), ]) @pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types) @pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types) @@ -90,6 +94,15 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): assert y.usm_type == usm_type_y +@pytest.mark.parametrize("usm_type_start", list_of_usm_types, ids=list_of_usm_types) +@pytest.mark.parametrize("usm_type_stop", list_of_usm_types, ids=list_of_usm_types) +def test_linspace_arrays(usm_type_start, usm_type_stop): + start = dp.asarray([0, 0], usm_type=usm_type_start) + stop = dp.asarray([2, 4], usm_type=usm_type_stop) + res = dp.linspace(start, stop, 4) + assert res.usm_type == du.get_coerced_usm_type([usm_type_start, usm_type_stop]) + + @pytest.mark.skip() @pytest.mark.parametrize("func", ["tril", "triu"], ids=["tril", "triu"]) @pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) diff --git a/tests/third_party/cupy/creation_tests/test_ranges.py b/tests/third_party/cupy/creation_tests/test_ranges.py index 4d5bc03f81b0..ac94297354f0 100644 --- a/tests/third_party/cupy/creation_tests/test_ranges.py +++ b/tests/third_party/cupy/creation_tests/test_ranges.py @@ -1,371 +1,371 @@ -import math -import sys -import unittest - -import numpy -import pytest - -import dpnp as cupy -from tests.third_party.cupy import testing - - -@testing.gpu -class TestRanges(unittest.TestCase): - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_arange(self, xp, dtype): - return xp.arange(10, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_arange2(self, xp, dtype): - return xp.arange(5, 10, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_arange3(self, xp, dtype): - return xp.arange(1, 11, 2, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_arange4(self, xp, dtype): - return xp.arange(20, 2, -3, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_arange5(self, xp, dtype): - return xp.arange(0, 100, None, dtype=dtype) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_arange6(self, xp, dtype): - return xp.arange(0, 2, dtype=dtype) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_arange7(self, xp, dtype): - return xp.arange(10, 11, dtype=dtype) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_arange8(self, xp, dtype): - return xp.arange(10, 8, -1, dtype=dtype) - - def test_arange9(self): - for xp in (numpy, cupy): - with pytest.raises((ValueError, TypeError)): - xp.arange(10, dtype=xp.bool_) - - @testing.numpy_cupy_array_equal() - def test_arange_no_dtype_int(self, xp): - return xp.arange(1, 11, 2) - - @testing.numpy_cupy_array_equal() - def test_arange_no_dtype_float(self, xp): - return xp.arange(1.0, 11.0, 2.0) - - @testing.numpy_cupy_array_equal() - def test_arange_negative_size(self, xp): - return xp.arange(3, 1) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace(self, xp, dtype): - return xp.linspace(0, 10, 5, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace2(self, xp, dtype): - return xp.linspace(10, 0, 5, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_zero_num(self, xp, dtype): - return xp.linspace(0, 10, 0, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_zero_num_no_endopoint_with_retstep(self, xp, dtype): - x, step = xp.linspace(0, 10, 0, dtype=dtype, endpoint=False, - retstep=True) - self.assertTrue(math.isnan(step)) - return x - - @testing.with_requires('numpy>=1.18') - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_one_num_no_endopoint_with_retstep(self, xp, dtype): - start, stop = 3, 7 - x, step = xp.linspace(start, stop, 1, dtype=dtype, endpoint=False, - retstep=True) - self.assertEqual(step, stop - start) - return x - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_one_num(self, xp, dtype): - return xp.linspace(0, 2, 1, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_no_endpoint(self, xp, dtype): - return xp.linspace(0, 10, 5, dtype=dtype, endpoint=False) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_with_retstep(self, xp, dtype): - x, step = xp.linspace(0, 10, 5, dtype=dtype, retstep=True) - self.assertEqual(step, 2.5) - return x - - @testing.numpy_cupy_allclose() - def test_linspace_no_dtype_int(self, xp): - return xp.linspace(0, 10) - - @testing.numpy_cupy_allclose() - def test_linspace_no_dtype_float(self, xp): - return xp.linspace(0.0, 10.0) - - @testing.numpy_cupy_allclose() - def test_linspace_float_args_with_int_dtype(self, xp): - return xp.linspace(0.1, 9.1, 11, dtype=int) - - def test_linspace_neg_num(self): - for xp in (numpy, cupy): - with pytest.raises(ValueError): - xp.linspace(0, 10, -1) - - @testing.numpy_cupy_allclose() - def test_linspace_float_overflow(self, xp): - return xp.linspace(0., sys.float_info.max / 5, 10, dtype=float) - - @testing.numpy_cupy_array_equal() - def test_linspace_float_underflow(self, xp): - # find minimum subnormal number - x = sys.float_info.min - while x / 2 > 0: - x /= 2 - return xp.linspace(0., x, 10, dtype=float) - - @testing.with_requires('numpy>=1.16') - @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), - no_bool=True, no_complex=True) - @testing.numpy_cupy_array_equal() - def test_linspace_array_start_stop(self, xp, dtype_range, dtype_out): - start = xp.array([0, 120], dtype=dtype_range) - stop = xp.array([100, 0], dtype=dtype_range) - return xp.linspace(start, stop, num=50, dtype=dtype_out) - - @testing.with_requires('numpy>=1.16') - @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), - no_bool=True, no_complex=True) - @testing.numpy_cupy_array_equal() - def test_linspace_mixed_start_stop(self, xp, dtype_range, dtype_out): - start = 0.0 - if xp.dtype(dtype_range).kind in 'u': - stop = xp.array([100, 16], dtype=dtype_range) - else: - stop = xp.array([100, -100], dtype=dtype_range) - return xp.linspace(start, stop, num=50, dtype=dtype_out) - - @testing.with_requires('numpy>=1.16') - @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), - no_bool=True, no_complex=True) - @testing.numpy_cupy_array_equal() - def test_linspace_mixed_start_stop2(self, xp, dtype_range, dtype_out): - if xp.dtype(dtype_range).kind in 'u': - start = xp.array([160, 120], dtype=dtype_range) - else: - start = xp.array([-120, 120], dtype=dtype_range) - stop = 0 - return xp.linspace(start, stop, num=50, dtype=dtype_out) - - @testing.with_requires('numpy>=1.16') - @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), - no_bool=True, no_complex=True) - @testing.numpy_cupy_array_equal() - def test_linspace_array_start_stop_axis1(self, xp, dtype_range, dtype_out): - start = xp.array([0, 120], dtype=dtype_range) - stop = xp.array([100, 0], dtype=dtype_range) - return xp.linspace(start, stop, num=50, dtype=dtype_out, axis=1) - - @testing.with_requires('numpy>=1.16') - @testing.for_complex_dtypes() - @testing.numpy_cupy_array_equal() - def test_linspace_complex_start_stop(self, xp, dtype): - start = xp.array([0, 120], dtype=dtype) - stop = xp.array([100, 0], dtype=dtype) - return xp.linspace(start, stop, num=50, dtype=dtype) - - @testing.with_requires('numpy>=1.16') - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_array_equal() - def test_linspace_start_stop_list(self, xp, dtype): - start = [0, 0] - stop = [100, 16] - return xp.linspace(start, stop, num=50, dtype=dtype) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace(self, xp, dtype): - return xp.logspace(0, 2, 5, dtype=dtype) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace2(self, xp, dtype): - return xp.logspace(2, 0, 5, dtype=dtype) - - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace_zero_num(self, xp, dtype): - return xp.logspace(0, 2, 0, dtype=dtype) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace_one_num(self, xp, dtype): - return xp.logspace(0, 2, 1, dtype=dtype) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace_no_endpoint(self, xp, dtype): - return xp.logspace(0, 2, 5, dtype=dtype, endpoint=False) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.numpy_cupy_allclose() - def test_logspace_no_dtype_int(self, xp): - return xp.logspace(0, 2) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.numpy_cupy_allclose() - def test_logspace_no_dtype_float(self, xp): - return xp.logspace(0.0, 2.0) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.numpy_cupy_allclose() - def test_logspace_float_args_with_int_dtype(self, xp): - return xp.logspace(0.1, 2.1, 11, dtype=int) - - def test_logspace_neg_num(self): - for xp in (numpy, cupy): - with pytest.raises(ValueError): - xp.logspace(0, 10, -1) - - @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_all_dtypes(no_bool=True) - @testing.numpy_cupy_allclose() - def test_logspace_base(self, xp, dtype): - return xp.logspace(0, 2, 5, base=2.0, dtype=dtype) - - -@testing.parameterize( - *testing.product({ - 'indexing': ['xy', 'ij'], - 'sparse': [False, True], - 'copy': [False, True], - }) -) -@testing.gpu -class TestMeshgrid(unittest.TestCase): - - @testing.for_all_dtypes() - def test_meshgrid0(self, dtype): - out = cupy.meshgrid(indexing=self.indexing, sparse=self.sparse, - copy=self.copy) - assert(out == []) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_meshgrid1(self, xp, dtype): - x = xp.arange(2).astype(dtype) - return xp.meshgrid(x, indexing=self.indexing, sparse=self.sparse, - copy=self.copy) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_meshgrid2(self, xp, dtype): - x = xp.arange(2).astype(dtype) - y = xp.arange(3).astype(dtype) - return xp.meshgrid(x, y, indexing=self.indexing, sparse=self.sparse, - copy=self.copy) - - @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal() - def test_meshgrid3(self, xp, dtype): - x = xp.arange(2).astype(dtype) - y = xp.arange(3).astype(dtype) - z = xp.arange(4).astype(dtype) - return xp.meshgrid(x, y, z, indexing=self.indexing, sparse=self.sparse, - copy=self.copy) - - -@testing.gpu -class TestMgrid(unittest.TestCase): - - @testing.numpy_cupy_array_equal() - def test_mgrid0(self, xp): - return xp.mgrid[0:] - - @testing.numpy_cupy_array_equal() - def test_mgrid1(self, xp): - return xp.mgrid[-10:10] - - @testing.numpy_cupy_array_equal() - def test_mgrid2(self, xp): - return xp.mgrid[-10:10:10j] - - @testing.numpy_cupy_array_equal() - def test_mgrid3(self, xp): - x = xp.zeros(10)[:, None] - y = xp.ones(10)[:, None] - return xp.mgrid[x:y:10j] - - @testing.numpy_cupy_array_equal() - def test_mgrid4(self, xp): - # check len(keys) > 1 - return xp.mgrid[-10:10:10j, -10:10:10j] - - @testing.numpy_cupy_array_equal() - def test_mgrid5(self, xp): - # check len(keys) > 1 - x = xp.zeros(10)[:, None] - y = xp.ones(10)[:, None] - return xp.mgrid[x:y:10j, x:y:10j] - - -@testing.gpu -class TestOgrid(unittest.TestCase): - - @testing.numpy_cupy_array_equal() - def test_ogrid0(self, xp): - return xp.ogrid[0:] - - @testing.numpy_cupy_array_equal() - def test_ogrid1(self, xp): - return xp.ogrid[-10:10] - - @testing.numpy_cupy_array_equal() - def test_ogrid2(self, xp): - return xp.ogrid[-10:10:10j] - - @testing.numpy_cupy_array_equal() - def test_ogrid3(self, xp): - x = xp.zeros(10)[:, None] - y = xp.ones(10)[:, None] - return xp.ogrid[x:y:10j] - - @testing.numpy_cupy_array_equal() - def test_ogrid4(self, xp): - # check len(keys) > 1 - return xp.ogrid[-10:10:10j, -10:10:10j] - - @testing.numpy_cupy_array_equal() - def test_ogrid5(self, xp): - # check len(keys) > 1 - x = xp.zeros(10)[:, None] - y = xp.ones(10)[:, None] - return xp.ogrid[x:y:10j, x:y:10j] +import math +import sys +import unittest + +import numpy +import pytest + +import dpnp as cupy +from tests.third_party.cupy import testing + + +@testing.gpu +class TestRanges(unittest.TestCase): + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_arange(self, xp, dtype): + return xp.arange(10, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_arange2(self, xp, dtype): + return xp.arange(5, 10, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_arange3(self, xp, dtype): + return xp.arange(1, 11, 2, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_arange4(self, xp, dtype): + return xp.arange(20, 2, -3, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_arange5(self, xp, dtype): + return xp.arange(0, 100, None, dtype=dtype) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_arange6(self, xp, dtype): + return xp.arange(0, 2, dtype=dtype) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_arange7(self, xp, dtype): + return xp.arange(10, 11, dtype=dtype) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_arange8(self, xp, dtype): + return xp.arange(10, 8, -1, dtype=dtype) + + def test_arange9(self): + for xp in (numpy, cupy): + with pytest.raises((ValueError, TypeError)): + xp.arange(10, dtype=xp.bool_) + + @testing.numpy_cupy_array_equal() + def test_arange_no_dtype_int(self, xp): + return xp.arange(1, 11, 2) + + @testing.numpy_cupy_array_equal() + def test_arange_no_dtype_float(self, xp): + return xp.arange(1.0, 11.0, 2.0) + + @testing.numpy_cupy_array_equal() + def test_arange_negative_size(self, xp): + return xp.arange(3, 1) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace(self, xp, dtype): + return xp.linspace(0, 10, 5, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace2(self, xp, dtype): + return xp.linspace(10, 0, 5, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_zero_num(self, xp, dtype): + return xp.linspace(0, 10, 0, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_zero_num_no_endopoint_with_retstep(self, xp, dtype): + x, step = xp.linspace(0, 10, 0, dtype=dtype, endpoint=False, + retstep=True) + self.assertTrue(math.isnan(step)) + return x + + @testing.with_requires('numpy>=1.18') + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_one_num_no_endopoint_with_retstep(self, xp, dtype): + start, stop = 3, 7 + x, step = xp.linspace(start, stop, 1, dtype=dtype, endpoint=False, + retstep=True) + self.assertEqual(step, stop - start) + return x + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_one_num(self, xp, dtype): + return xp.linspace(0, 2, 1, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_linspace_no_endpoint(self, xp, dtype): + return xp.linspace(0, 10, 5, dtype=dtype, endpoint=False) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_with_retstep(self, xp, dtype): + x, step = xp.linspace(0, 10, 5, dtype=dtype, retstep=True) + self.assertEqual(step, 2.5) + return x + + @testing.numpy_cupy_allclose() + def test_linspace_no_dtype_int(self, xp): + return xp.linspace(0, 10, 50) + + @testing.numpy_cupy_allclose() + def test_linspace_no_dtype_float(self, xp): + return xp.linspace(0.0, 10.0, 50) + + @testing.numpy_cupy_array_equal() + def test_linspace_float_args_with_int_dtype(self, xp): + return xp.linspace(0.1, 9.1, 11, dtype=int) + + def test_linspace_neg_num(self): + for xp in (numpy, cupy): + with pytest.raises(ValueError): + xp.linspace(0, 10, -1) + + @testing.numpy_cupy_allclose() + def test_linspace_float_overflow(self, xp): + return xp.linspace(0., sys.float_info.max / 5, 10, dtype=float) + + @testing.numpy_cupy_array_equal() + def test_linspace_float_underflow(self, xp): + # find minimum subnormal number + x = sys.float_info.min + while x / 2 > 0: + x /= 2 + return xp.linspace(0., x, 10, dtype=float) + + @testing.with_requires('numpy>=1.16') + @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), + no_bool=True, no_complex=True) + @testing.numpy_cupy_allclose() + def test_linspace_array_start_stop(self, xp, dtype_range, dtype_out): + start = xp.array([0, 120], dtype=dtype_range) + stop = xp.array([100, 0], dtype=dtype_range) + return xp.linspace(start, stop, num=50, dtype=dtype_out) + + @testing.with_requires('numpy>=1.16') + @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), + no_bool=True, no_complex=True) + @testing.numpy_cupy_array_equal() + def test_linspace_mixed_start_stop(self, xp, dtype_range, dtype_out): + start = 0.0 + if xp.dtype(dtype_range).kind in 'u': + stop = xp.array([100, 16], dtype=dtype_range) + else: + stop = xp.array([100, -100], dtype=dtype_range) + return xp.linspace(start, stop, num=50, dtype=dtype_out) + + @testing.with_requires('numpy>=1.16') + @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), + no_bool=True, no_complex=True) + @testing.numpy_cupy_allclose() + def test_linspace_mixed_start_stop2(self, xp, dtype_range, dtype_out): + if xp.dtype(dtype_range).kind in 'u': + start = xp.array([160, 120], dtype=dtype_range) + else: + start = xp.array([-120, 120], dtype=dtype_range) + stop = 0 + return xp.linspace(start, stop, num=50, dtype=dtype_out) + + @testing.with_requires('numpy>=1.16') + @testing.for_all_dtypes_combination(names=('dtype_range', 'dtype_out'), + no_bool=True, no_complex=True) + @testing.numpy_cupy_array_equal() + def test_linspace_array_start_stop_axis1(self, xp, dtype_range, dtype_out): + start = xp.array([0, 120], dtype=dtype_range) + stop = xp.array([100, 0], dtype=dtype_range) + return xp.linspace(start, stop, num=50, dtype=dtype_out, axis=1) + + @testing.with_requires('numpy>=1.16') + @testing.for_complex_dtypes() + @testing.numpy_cupy_array_equal() + def test_linspace_complex_start_stop(self, xp, dtype): + start = xp.array([0, 120], dtype=dtype) + stop = xp.array([100, 0], dtype=dtype) + return xp.linspace(start, stop, num=50, dtype=dtype) + + @testing.with_requires('numpy>=1.16') + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_array_equal() + def test_linspace_start_stop_list(self, xp, dtype): + start = [0, 0] + stop = [100, 16] + return xp.linspace(start, stop, num=50, dtype=dtype) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace(self, xp, dtype): + return xp.logspace(0, 2, 5, dtype=dtype) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace2(self, xp, dtype): + return xp.logspace(2, 0, 5, dtype=dtype) + + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace_zero_num(self, xp, dtype): + return xp.logspace(0, 2, 0, dtype=dtype) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace_one_num(self, xp, dtype): + return xp.logspace(0, 2, 1, dtype=dtype) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace_no_endpoint(self, xp, dtype): + return xp.logspace(0, 2, 5, dtype=dtype, endpoint=False) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_allclose() + def test_logspace_no_dtype_int(self, xp): + return xp.logspace(0, 2) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_allclose() + def test_logspace_no_dtype_float(self, xp): + return xp.logspace(0.0, 2.0) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_allclose() + def test_logspace_float_args_with_int_dtype(self, xp): + return xp.logspace(0.1, 2.1, 11, dtype=int) + + def test_logspace_neg_num(self): + for xp in (numpy, cupy): + with pytest.raises(ValueError): + xp.logspace(0, 10, -1) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.for_all_dtypes(no_bool=True) + @testing.numpy_cupy_allclose() + def test_logspace_base(self, xp, dtype): + return xp.logspace(0, 2, 5, base=2.0, dtype=dtype) + + +@testing.parameterize( + *testing.product({ + 'indexing': ['xy', 'ij'], + 'sparse': [False, True], + 'copy': [False, True], + }) +) +@testing.gpu +class TestMeshgrid(unittest.TestCase): + + @testing.for_all_dtypes() + def test_meshgrid0(self, dtype): + out = cupy.meshgrid(indexing=self.indexing, sparse=self.sparse, + copy=self.copy) + assert(out == []) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_meshgrid1(self, xp, dtype): + x = xp.arange(2).astype(dtype) + return xp.meshgrid(x, indexing=self.indexing, sparse=self.sparse, + copy=self.copy) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_meshgrid2(self, xp, dtype): + x = xp.arange(2).astype(dtype) + y = xp.arange(3).astype(dtype) + return xp.meshgrid(x, y, indexing=self.indexing, sparse=self.sparse, + copy=self.copy) + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_meshgrid3(self, xp, dtype): + x = xp.arange(2).astype(dtype) + y = xp.arange(3).astype(dtype) + z = xp.arange(4).astype(dtype) + return xp.meshgrid(x, y, z, indexing=self.indexing, sparse=self.sparse, + copy=self.copy) + + +@testing.gpu +class TestMgrid(unittest.TestCase): + + @testing.numpy_cupy_array_equal() + def test_mgrid0(self, xp): + return xp.mgrid[0:] + + @testing.numpy_cupy_array_equal() + def test_mgrid1(self, xp): + return xp.mgrid[-10:10] + + @testing.numpy_cupy_array_equal() + def test_mgrid2(self, xp): + return xp.mgrid[-10:10:10j] + + @testing.numpy_cupy_array_equal() + def test_mgrid3(self, xp): + x = xp.zeros(10)[:, None] + y = xp.ones(10)[:, None] + return xp.mgrid[x:y:10j] + + @testing.numpy_cupy_array_equal() + def test_mgrid4(self, xp): + # check len(keys) > 1 + return xp.mgrid[-10:10:10j, -10:10:10j] + + @testing.numpy_cupy_array_equal() + def test_mgrid5(self, xp): + # check len(keys) > 1 + x = xp.zeros(10)[:, None] + y = xp.ones(10)[:, None] + return xp.mgrid[x:y:10j, x:y:10j] + + +@testing.gpu +class TestOgrid(unittest.TestCase): + + @testing.numpy_cupy_array_equal() + def test_ogrid0(self, xp): + return xp.ogrid[0:] + + @testing.numpy_cupy_array_equal() + def test_ogrid1(self, xp): + return xp.ogrid[-10:10] + + @testing.numpy_cupy_array_equal() + def test_ogrid2(self, xp): + return xp.ogrid[-10:10:10j] + + @testing.numpy_cupy_array_equal() + def test_ogrid3(self, xp): + x = xp.zeros(10)[:, None] + y = xp.ones(10)[:, None] + return xp.ogrid[x:y:10j] + + @testing.numpy_cupy_array_equal() + def test_ogrid4(self, xp): + # check len(keys) > 1 + return xp.ogrid[-10:10:10j, -10:10:10j] + + @testing.numpy_cupy_array_equal() + def test_ogrid5(self, xp): + # check len(keys) > 1 + x = xp.zeros(10)[:, None] + y = xp.ones(10)[:, None] + return xp.ogrid[x:y:10j, x:y:10j]