eye_like#

ivy.eye_like(x, *, k=0, dtype=None, device=None, out=None)[source]#

Return a 2D array filled with ones on the k diagonal and zeros elsewhere. having the same shape as the first and last dim of input array x. input array x should to be 2D.

Parameters:
  • x (Union[Array, NativeArray]) – input array from which to derive the output array shape.

  • 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.

  • 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 – an array having the same shape as x and filled with ones in diagonal k and zeros elsewhere.

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 ivy.Array input:

>>> x1 = ivy.array([[0, 1],[2, 3]])
>>> y1 = ivy.eye_like(x1)
>>> print(y1)
ivy.array([[1., 0.],
           [0., 1.]])
>>> x1 = ivy.array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])
>>> y1 = ivy.eye_like(x1, k=1)
>>> print(y1)
ivy.array([[0., 1., 0.],
           [0., 0., 1.],
           [0., 0., 0.]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[3, 8],[0, 2]]), b=ivy.array([[0, 2], [8, 5]]))
>>> y = x.eye_like()
>>> print(y)
{
    a: ivy.array([[1., 0.],
                  [0., 1.]]),
    b: ivy.array([[1., 0.],
                  [0., 1.]])
}
Array.eye_like(self, /, *, k=0, dtype=None, device=None, out=None)[source]#

ivy.Array instance method variant of ivy.eye_like. This method simply wraps the function, and so the docstring for ivy.eye_like also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array from which to derive the output array shape.

  • 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.

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

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to place the created array. If device is None, the output array device must be inferred from self. Default: None.

  • 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 – an array having the same shape as self and filled with ones in diagonal k and zeros elsewhere.

Examples

>>> x = ivy.array([[2, 3, 8],[1, 2, 1]])
>>> y = x.eye_like()
>>> print(y)
ivy.array([[1., 0., 0.],
            0., 1., 0.]])
Container.eye_like(self, /, k=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, out=None, dtype=None, device=None)[source]#

ivy.Container instance method variant of ivy.eye_like. This method simply wraps the function, and so the docstring for ivy.eye_like also applies to this method with minimal changes.

Parameters:
  • self (Container) – input array or container from which to derive the output container shape.

  • k (Union[int, Container], 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.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • dtype (Optional[Union[Dtype, NativeDtype, Container]], default: None) – output array data type. If dtype is None, the output container data type must be inferred from self. Default: None.

  • device (Optional[Union[Device, NativeDevice, Container]], default: None) – device on which to place the created array. If device is None, the output container device must be inferred from self. Default: None.

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

Return type:

Container

Returns:

ret – a container having the same shape as x and filled with ones in diagonal k and zeros elsewhere.

Examples

>>> x = ivy.Container(a=ivy.array([3., 8.]), b=ivy.array([2., 2.]))
>>> y = x.eye_like()
>>> print(y)
{
    a: ivy.array([[1.],
                  [0.]]),
    b: ivy.array([[1.],
                  [0.]])
}