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
shapeas the first and last dim of input arrayx. input arrayxshould 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
xand filled withonesin diagonalkandzeroselsewhere.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Containerinstances as a replacement to any of the arguments.Examples
With
ivy.Arrayinput:>>> 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.Containerinput:>>> 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. IfdtypeisNone, 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. IfdeviceisNone, 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
selfand filled withonesin diagonalkandzeroselsewhere.
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. IfdtypeisNone, 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
xand filled withonesin diagonalkandzeroselsewhere.
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.]]) }