ones#

ivy.ones(shape, *, dtype=None, device=None, out=None)[source]#

Return a new array having a specified shape and filled with ones.

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:
  • shape (Union[Shape, NativeShape]) – output array shape.

  • 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) – device on which to place the created array. 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 containing ones.

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.Shape input:

>>> shape = (2,2)
>>> x = ivy.ones(shape)
>>> print(x)
ivy.array([[1., 1.],
       [1., 1.]])

With ivy.Dtype input:

>>> shape = (3,2)
>>> d_type = ivy.int64
>>> y = ivy.ones(shape, dtype=d_type)
>>> print(y)
ivy.array([[1, 1],
       [1, 1],
       [1, 1]])

With ivy.Device input:

>>> shape = (3,2)
>>> y = ivy.ones(shape, device="cpu")
>>> print(y)
ivy.array([[1., 1.],
       [1., 1.],
       [1., 1.]])

With ivy.Array input:

>>> shape = (1, 5, 2)
>>> x = ivy.zeros(shape)
>>> ivy.ones(shape, out=x)
>>> print(x)
ivy.array([[[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.]]])