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 arrayx
. input arrayx
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:
- Returns:
ret – an array having the same shape as
x
and filled withones
in diagonalk
andzeros
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. Ifdtype
isNone
, the output array data type must be inferred fromself
. Default:None
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. Ifdevice
isNone
, the output array device must be inferred fromself
. 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 withones
in diagonalk
andzeros
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 isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.dtype (
Optional
[Union
[Dtype
,NativeDtype
,Container
]], default:None
) – output array data type. Ifdtype
isNone
, the output container data type must be inferred fromself
. Default:None
.device (
Optional
[Union
[Device
,NativeDevice
,Container
]], default:None
) – device on which to place the created array. If device isNone
, the output container device must be inferred fromself
. 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 withones
in diagonalk
andzeros
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.]]) }