eye#

ivy.eye(n_rows, n_cols=None, /, *, k=0, batch_shape=None, dtype=None, device=None, out=None)[source]#

Return a two-dimensional array with ones on the k diagonal and zeros elsewhere.

Parameters:
  • n_rows (int) – number of rows in the output array.

  • n_cols (Optional[int], default: None) – number of columns in the output array. If None, the default number of columns in the output array is equal to n_rows. Default: None.

  • k (int, default: 0) – index of the diagonal. A positive value refers to an upper diagonal, a negative value to a lower diagonal, and 0 to the main diagonal. Default: 0.

  • batch_shape (Optional[Union[int, Sequence[int]]], default: None) – optional input that determines returning identity array shape. Default: None.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type must be the default floating-point data type. Default: None.

  • device (Optional[Union[Device, NativeDevice]], default: None) – the device on which to place the created array.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – device on which to place the created array. Default: None.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances as a replacement to any of the arguments.

Examples

With :’n_rows’ input:

>>> x = ivy.eye(3)
>>> print(x)
ivy.array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])

With :’n_cols’ input:

>>> x = ivy.eye(3,4)
>>> print(x)
ivy.array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.]])

With :’k’ input:

>>> x = ivy.eye(3, k=1)
>>> print(x)
ivy.array([[0., 1., 0.],
           [0., 0., 1.],
           [0., 0., 0.]])

With :’dtype’ input:

>>> x = ivy.eye(4, k=2, dtype=ivy.IntDtype('int32'))
>>> print(x)
ivy.array([[0, 0, 1, 0],
           [0, 0, 0, 1],
           [0, 0, 0, 0],
           [0, 0, 0, 0]])

With :’batch_shape’ input:

>>> x = ivy.eye(2, 3, batch_shape=[3])
>>> print(x)
ivy.array([[[1., 0., 0.],
            [0., 1., 0.]],

[[1., 0., 0.], [0., 1., 0.]],

[[1., 0., 0.], [0., 1., 0.]]])

With :’out’ input:

>>> y = ivy.ones((3, 3))
>>> ivy.eye(3, out=y)
>>> print(y)
ivy.array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])

With :’device’ input:

>>> x = ivy.eye(3, device=ivy.Device('cpu'))
>>> print(x)
ivy.array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])