ones_like#
- ivy.ones_like(x, /, *, dtype=None, device=None, out=None)[source]#
Return a new array filled with ones and having the same shape as an input array
x
.Note
An output array having a complex floating-point data type must contain complex numbers having a real component equal to one and an imaginary component equal to zero (i.e.,
1 + 0j
).- Parameters:
x (
Union
[Array
,NativeArray
]) – input array from which to derive the output array shape.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type must be inferred fromx
. DefaultNone
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. If device isNone
, the output array device must be inferred fromx
. 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:
- Returns:
ret – an array having the same shape as
x
and filled withones
.
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 in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([1, 2, 3, 4, 5, 6]) >>> y = ivy.ones_like(x) >>> print(y) ivy.array([1, 1, 1, 1, 1, 1])
>>> x = ivy.array([[0, 1, 2],[3, 4, 5]], dtype = ivy.float32) >>> y = ivy.ones_like(x) >>> print(y) ivy.array([[1., 1., 1.], [1., 1., 1.]])
>>> x = ivy.array([3., 2., 1.]) >>> y = ivy.zeros(3) >>> ivy.ones_like(x, out=y) >>> print(y) ivy.array([1., 1., 1.])
With
ivy.NativeArray
input:>>> x = ivy.native_array([[3, 8, 2],[2, 8, 3]]) >>> y = ivy.ones_like(x) >>> print(y) ivy.array([[1, 1, 1], [1, 1, 1]])
>>> x = ivy.native_array([3, 8, 2, 0, 0, 2]) >>> y = ivy.ones_like(x, dtype=ivy.IntDtype('int32'), device=ivy.Device('cpu')) >>> print(y) ivy.array([1, 1, 1, 1, 1, 1])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([3, 2, 1]), b=ivy.array([8, 2, 3])) >>> y = ivy.ones_like(x) >>> print(y) { a: ivy.array([1, 1, 1]), b: ivy.array([1, 1, 1]) }
With
ivy.Array
input:>>> x = ivy.array([2, 3, 8, 2, 1]) >>> y = x.ones_like() >>> print(y) ivy.array([1, 1, 1, 1, 1])
With :class:’ivy.Container’ input:
>>> x = ivy.Container(a=ivy.array([3., 8.]), b=ivy.array([2., 2.])) >>> y = x.ones_like() >>> print(y) { a: ivy.array([1., 1.]), b: ivy.array([1., 1.]) }
- Array.ones_like(self, /, *, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.ones_like. This method simply wraps the function, and so the docstring for ivy.ones_like also applies to this method with minimal changes.
- Parameters:
self (
Array
) – input array from which to derive the output array shape.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type must be inferred fromself
. DefaultNone
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to place the created array. If device 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 with ones.
- Container.ones_like(self, /, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, dtype=None, device=None, out=None)[source]#
ivy.Container instance method variant of ivy.ones_like. This method simply wraps the function, and so the docstring for ivy.ones_like also applies to this method with minimal changes.
- Parameters:
self (
Container
) – input array from which to derive the output array shape.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 array data type must be inferred fromself
. DefaultNone
.device (
Optional
[Union
[Device
,NativeDevice
,Container
]], default:None
) – device on which to place the created array. If device isNone
, the output array device must be inferred fromself
. Default:None
.out (
Optional
[Container
], default:None
) – optional output array, 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
self
and filled with ones.