dpnp.indices

dpnp.indices(dimensions, dtype=<class 'int'>, sparse=False, device=None, usm_type='device', sycl_queue=None)[source]

Return an array representing the indices of a grid.

Compute an array where the subarrays contain index values 0, 1, … varying only along the corresponding axis.

For full documentation refer to numpy.indices.

Parameters:
  • dimensions (sequence of ints) -- The shape of the grid.

  • dtype ({None, dtype}, optional) -- Data type of the result.

  • sparse ({None, boolean}, optional) -- Return a sparse representation of the grid instead of a dense representation. Default is False.

  • device ({None, string, SyclDevice, SyclQueue}, optional) -- An array API concept of device where the output array is created. The device can be None (the default), an OneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a Device object returned by dpnp.dpnp_array.dpnp_array.device property.

  • usm_type ({"device", "shared", "host"}, optional) -- The type of SYCL USM allocation for the output array.

  • sycl_queue ({None, SyclQueue}, optional) -- A SYCL queue to use for output array allocation and copying.

Returns:

out -- If sparse is False: Returns one array of grid indices, grid.shape = (len(dimensions),) + tuple(dimensions).

If sparse is True: Returns a tuple of arrays, with grid[i].shape = (1, ..., 1, dimensions[i], 1, ..., 1) with dimensions[i] in the i-th place.

Return type:

one dpnp.ndarray or tuple of dpnp.ndarray

Examples

>>> import dpnp as np
>>> grid = np.indices((2, 3))
>>> grid.shape
(2, 2, 3)
>>> grid[0]
array([[0, 0, 0],
       [1, 1, 1]])
>>> grid[1]
array([[0, 1, 2],
       [0, 1, 2]])

The indices can be used as an index into an array.

>>> x = np.arange(20).reshape(5, 4)
>>> row, col = np.indices((2, 3))
>>> x[row, col]
array([[0, 1, 2],
       [4, 5, 6]])

Note that it would be more straightforward in the above example to extract the required elements directly with x[:2, :3]. If sparse is set to True, the grid will be returned in a sparse representation.

>>> i, j = np.indices((2, 3), sparse=True)
>>> i.shape
(2, 1)
>>> j.shape
(1, 3)
>>> i
array([[0],
       [1]])
>>> j
array([[0, 1, 2]])