dpnp.eye

dpnp.eye(N, /, M=None, k=0, dtype=None, order='C', *, like=None, device=None, usm_type='device', sycl_queue=None)[source]

Return a 2-D array with ones on the diagonal and zeros elsewhere.

For full documentation refer to numpy.eye.

Parameters:
Nint

Number of rows in the output.

M{None, int}, optional

Number of columns in the output. If None, defaults to N.

Default: None.

kint, optional

Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.

Default: 0.

dtype{None, str, dtype object}, optional

The desired dtype for the array, e.g., dpnp.int32. If None, uses a default floating point data type for the device on which the input array is allocated.

Default: None.

order{None, "C", "F"}, optional

Memory layout of the newly output array.

Default: "C".

device{None, string, SyclDevice, SyclQueue, Device}, optional

An array API concept of device where the output array is created. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device.

Default: None.

usm_type{None, "device", "shared", "host"}, optional

The type of SYCL USM allocation for the output array.

Default: "device".

sycl_queue{None, SyclQueue}, optional

A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as None (the default), which means to get the SYCL queue from device keyword if present or to use a default queue.

Default: None.

Returns:
outdpnp.ndarray

An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

Limitations

Parameter like is supported only with default value None. Otherwise, the function raises NotImplementedError exception.

See also

dpnp.identity

Return the identity array.

dpnp.diag

Extract a diagonal or construct a diagonal array.

Examples

>>> import dpnp as np
>>> np.eye(2, dtype=int)
array([[1, 0],
       [0, 1]])
>>> np.eye(3, k=1)
array([[0.,  1.,  0.],
       [0.,  0.,  1.],
       [0.,  0.,  0.]])

Creating an array on a different device or with a specified usm_type

>>> x = np.eye(2, dtype=int) # default case
>>> x, x.device, x.usm_type
(array([[1, 0],
        [0, 1]]), Device(level_zero:gpu:0), 'device')
>>> y = np.eye(2, dtype=int, device="cpu")
>>> y, y.device, y.usm_type
(array([[1, 0],
        [0, 1]]), Device(opencl:cpu:0), 'device')
>>> z = np.eye(2, dtype=int, usm_type="host")
>>> z, z.device, z.usm_type
(array([[1, 0],
        [0, 1]]), Device(level_zero:gpu:0), 'host')