Data type#

class ivy.data_classes.array.data_type._ArrayWithDataTypes[source]#

Bases: ABC

_abc_impl = <_abc._abc_data object>#
astype(dtype, /, *, copy=True, out=None)[source]#

Copy an array to a specified data type irrespective of type- promotion rules.

Casting floating-point NaN and infinity values to integral data types is not specified and is implementation-dependent.

When casting a boolean input array to a numeric data type, a value of True must cast to a numeric value equal to 1, and a value of False must cast to a numeric value equal to 0.

When casting a numeric input array to bool, a value of 0 must cast to False, and a non-zero value must cast to True.

Parameters:
  • self (Array) – array to cast.

  • dtype (Dtype) – desired data type.

  • copy (bool, default: True) – specifies whether to copy an array when the specified dtype matches the data type of the input array x. If True, a newly allocated array must always be returned. If False and the specified dtype matches the data type of the input array, the input array must be returned; otherwise, a newly allocated must be returned. Default: True.

  • 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 specified data type. The returned array must have the same shape as x.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([[-1, -2], [0, 2]])
>>> print(x.astype(ivy.float64))
ivy.array([[-1., -2.],  [0.,  2.]])
broadcast_arrays(*arrays)[source]#

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

Parameters:
  • self (Array) – An input array to be broadcasted against other input arrays.

  • arrays (Union[Array, NativeArray]) – an arbitrary number of arrays to-be broadcasted. Each array must have the same shape. Each array must have the same dtype as its corresponding input array.

Return type:

List[Array]

Returns:

ret – A list containing broadcasted arrays of type ivy.Array

Examples

With ivy.Array inputs:

>>> x1 = ivy.array([1, 2])
>>> x2 = ivy.array([0.2, 0.])
>>> x3 = ivy.zeros(2)
>>> y = x1.broadcast_arrays(x2, x3)
>>> print(y)
[ivy.array([1, 2]), ivy.array([0.2, 0. ]), ivy.array([0., 0.])]

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([-1., 3.4])
>>> x2 = ivy.native_array([2.4, 5.1])
>>> y = x1.broadcast_arrays(x2)
>>> print(y)
[ivy.array([-1., 3.4]), ivy.array([2.4, 5.1])]
broadcast_to(shape, *, out=None)[source]#

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

Parameters:
  • self (Array) – input array to be broadcasted.

  • shape (Tuple[int, ...]) – desired shape to be broadcasted to.

  • out (Optional[Array], default: None) – Optional array to store the broadcasted array.

Return type:

Array

Returns:

ret – Returns the broadcasted array of shape ‘shape’

Examples

With ivy.Array instance method:

>>> x = ivy.array([1, 2, 3])
>>> y = x.broadcast_to((3,3))
>>> print(y)
ivy.array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
can_cast(to)[source]#

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

Parameters:
  • self (Array) – input array from which to cast.

  • to (Dtype) – desired data type.

Return type:

bool

Returns:

retTrue if the cast can occur according to type-promotion rules; otherwise, False.

Examples

>>> x = ivy.array([1., 2., 3.])
>>> print(x.dtype)
float32
>>> x = ivy.array([4., 5., 6.])
>>> print(x.can_cast(ivy.float64))
True
dtype(as_native=False)[source]#

ivy.Array instance method variant of ivy.dtype. This method helps to get the data type of the array.

Parameters:
  • self (Array) – The input array.

  • as_native (bool, default: False) – Whether to return the native data type of the array. If True, returns the native data type. Default is False.

Return type:

Union[Dtype, NativeDtype]

Returns:

ret – The data type of the array. If as_native is True, returns the native data type.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = x.dtype()
>>> print(y)
int32
>>> x= ivy.array([1.0, 2.0, 3.0], dtype=ivy.float64)
>>> y = x.dtype(as_native=True)
>>> print(y)
float64
finfo()[source]#

Array instance method variant of ivy.finfo.

Parameters:

self (Array) – input array.

Return type:

None

Returns:

ret – An instance of the Finfo class, containing information about the floating point data type of the input array.

Example

>>> x = ivy.array([0.7,8.4,3.14], dtype=ivy.float32)
>>> print(x.finfo())
finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
iinfo()[source]#

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

Parameters:

self (Array) – input array.

Return type:

None

Returns:

ret – An instance of the Iinfo class, containing information about the integer data type of the input array.

Examples

>>> x = ivy.array([-119,122,14], dtype=ivy.int8))
>>> x.iinfo()
iinfo(min=-128, max=127, dtype=int8)
>>> x = ivy.array([-12,54,1,9,-1220], dtype=ivy.int16))
>>> x.iinfo()
iinfo(min=-32768, max=32767, dtype=int16)
is_bool_dtype()[source]#
Return type:

bool

is_float_dtype()[source]#

ivy.Array instance method variant of ivy.is_float_dtype. This method simply checks to see if the array is of type float.

Parameters:

self (Array) – Input array from which to check for float dtype.

Return type:

bool

Returns:

ret – Boolean value of whether the array is of type float.

Examples

>>> x = ivy.array([1, 2, 3], dtype=ivy.int8)
>>> print(x.is_float_dtype())
False
>>> x = ivy.array([2.3, 4.5, 6.8], dtype=ivy.float32)
>>> print( x.is_float_dtype())
True
is_int_dtype()[source]#
Return type:

bool

is_uint_dtype()[source]#
Return type:

bool

result_type(*arrays_and_dtypes)[source]#

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

Parameters:
  • self (Array) – input array from which to cast.

  • arrays_and_dtypes (Union[Array, NativeArray, Dtype]) – an arbitrary number of input arrays and/or dtypes.

Return type:

Dtype

Returns:

ret – the dtype resulting from an operation involving the input arrays and dtypes.

Examples

>>> x = ivy.array([0, 1, 2])
>>> print(x.dtype)
int32
>>> x.result_type(ivy.float64)
<dtype:'float64'>