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]) – boolean, indicating whether or not to copy the input. Default: None. (default: None)

  • dtype (Optional[Union[Dtype, NativeDtype]]) – output array data type. If dtype is None, the output array data type must (default: None) be the default floating-point data type. Default None.

  • device (Optional[Union[Device, NativeDevice]]) – device on which to place the created array. Default: None. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type:

Array

Returns:

ret – An array interpretation of x.

Functional 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]])

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 # noqa 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)#

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]) – boolean, indicating whether or not to copy the input. Default: None. (default: None)

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

  • device (Optional[Union[Device, NativeDevice]]) – device on which to place the created array. Default: None. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that (default: None) 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)#
Return type:

Container