asarray#

ivy.asarray(obj, /, *, copy=None, dtype=None, device=None, out=None)[source]#

Convert the input to an array.

Parameters:
  • obj (Union[Array, NativeArray, Shape, NativeShape, bool, int, float, NestedSequence, TypeVar(SupportsBufferProtocol), ndarray]) – input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

  • copy (Optional[bool], default: None) – boolean, indicating whether or not to copy the input. Default: None.

  • 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 interpretation of x.

Examples

With list of lists as input:

>>> ivy.asarray([[1,2],[3,4]])
ivy.array([[1, 2],
           [3, 4]])

With tuple of lists as input:

>>> ivy.asarray(([1.4,5.6,5.5],[3.1,9.1,7.5]))
ivy.array([[1.39999998, 5.5999999 , 5.5       ],
           [3.0999999 , 9.10000038, 7.5       ]])

With ndarray as input:

>>> x = ivy.np.ndarray(shape=(2,2), order='C')
>>> ivy.asarray(x)
ivy.array([[6.90786433e-310, 6.90786433e-310],
           [6.90786433e-310, 6.90786433e-310]])

With ivy.Container as input:

>>> x = ivy.Container(a = [(1,2),(3,4),(5,6)], b = ((1,2,3),(4,5,6)))
>>> ivy.asarray(x)
{
    a: ivy.array([[1, 2],[3, 4], [5, 6]]),
    b: ivy.array([[1, 2, 3],
               [4, 5, 6]])
}

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.

Array.asarray(self, /, *, copy=None, dtype=None, device=None, out=None)[source]#

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

Parameters:
  • self (Array) – input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default: None.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – datatype, optional. Datatype is inferred from the input data.

  • 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 interpretation of self.

Examples

With list of lists as input:

>>> ivy.asarray([[1,2],[3,4]])
ivy.array([[1, 2],
       [3, 4]])

With tuple of lists as input:

>>> ivy.asarray(([1.4,5.6,5.5],[3.1,9.1,7.5]))
ivy.array([[1.39999998, 5.5999999 , 5.5       ],
       [3.0999999 , 9.10000038, 7.5       ]])

With ndarray as input:

>>> x = ivy.np.ndarray(shape=(2,2), order='C')
>>> x
array([[6.90786433e-310, 6.90786433e-310],
       [6.90786433e-310, 6.90786433e-310]])
>>> ivy.asarray(x)
ivy.array([[6.90786433e-310, 6.90786433e-310],
       [6.90786433e-310, 6.90786433e-310]])
Container.asarray(self, /, copy=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, dtype=None, device=None, out=None)[source]#
Return type:

Container