Data type

class ivy.DefaultDtype(dtype)[source]

Bases: object

__init__(dtype)[source]
class ivy.DefaultFloatDtype(float_dtype)[source]

Bases: object

__init__(float_dtype)[source]
class ivy.DefaultIntDtype(float_dtype)[source]

Bases: object

__init__(float_dtype)[source]
class ivy.DefaultUintDtype(uint_dtype)[source]

Bases: object

__init__(uint_dtype)[source]
ivy.as_ivy_dtype(dtype_in, /)[source]

Convert native data type to string representation.

Parameters

dtype_in (Union[Dtype, str]) – The data type to convert to string.

Return type

Dtype

Returns

ret – data type string ‘float32’

ivy.as_native_dtype(dtype_in, /)[source]

Convert data type string representation to native data type.

Parameters

dtype_in (Union[Dtype, NativeDtype]) – The data type string to convert to native data type.

Return type

NativeDtype

Returns

ret – data type e.g. ivy.float32.

ivy.astype(x, dtype, /, *, copy=True, out=None)[source]

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

Note

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

Note

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
  • x (Union[Array, NativeArray]) – array to cast.

  • dtype (Union[Dtype, NativeDtype]) – desired data type.

  • copy (bool) – specifies whether to copy an array when the specified dtype matches the data (default: True) 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]) – 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 having the specified data type. The returned array must have the same shape as x.

Examples

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

Broadcasts one or more arrays against one another.

Parameters

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

>>> x1 = ivy.array([1, 2, 3])
>>> x2 = ivy.array([4, 5, 6])
>>> y = ivy.broadcast_arrays(x1, x2)
>>> print(y)
[ivy.array([1, 2, 3]), ivy.array([4, 5, 6])]

With ivy.NativeArray inputs:

>>> x1 = ivy.native_array([0.3, 4.3])
>>> x2 = ivy.native_array([3.1, 5])
>>> x3 = ivy.native_array([2, 0])
>>> y = ivy.broadcast_arrays(x1, x2, x3)
[ivy.array([0.3, 4.3]), ivy.array([3.1, 5.]), ivy.array([2, 0])]

With mixed ivy.Array and ivy.NativeArray inputs:

>>> x1 = ivy.array([1, 2])
>>> x2 = ivy.native_array([0.3, 4.3])
>>> y = ivy.broadcast_arrays(x1, x2)
>>> print(y)
[ivy.array([1, 2]), ivy.array([0.3, 4.3])]

With ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([3, 1]), b=ivy.zeros(2))
>>> x2 = ivy.Container(a=ivy.array([4, 5]), b=ivy.array([2, -1]))
>>> y = ivy.broadcast_arrays(x1, x2)
>>> print(y)
[{
    a: ivy.array([3, 1]),
    b: ivy.array([0., 0.])
}, {
    a: ivy.array([4, 5]),
    b: ivy.array([2, -1])
}]

With mixed ivy.Array and ivy.Container inputs:

>>> x1 = ivy.zeros(2)
>>> x2 = ivy.Container(a=ivy.array([4, 5]), b=ivy.array([2, -1]))
>>> y = ivy.broadcast_arrays(x1, x2)
>>> print(y)
[{
    a: ivy.array([0., 0.]),
    b: ivy.array([0., 0.])
}, {
    a: ivy.array([4, 5]),
    b: ivy.array([2, -1])
}]
ivy.broadcast_to(x, /, shape, *, out=None)[source]

Broadcasts an array to a specified shape.

Parameters
  • x (Union[Array, NativeArray]) – array to broadcast.

  • shape (Tuple[int, ...]) – array shape. Must be compatible with x (see Broadcasting). If the array is incompatible with the specified shape, the function should raise an exception.

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

Return type

Array

Returns

ret – an array having a specified shape. Must have the same data type as x.

Examples

With ivy.Array input:

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

With ivy.NativeArray input:

>>> x = ivy.native_array([0.1 , 0.3])
>>> y = ivy.broadcast_to(x, (3, 2))
>>> print(y)
ivy.array([[0.1, 0.3],
           [0.1, 0.3],
           [0.1, 0.3]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([1, 2, 3]),        b=ivy.array([4, 5, 6]))
>>> y = ivy.broadcast_to(x, (3, 3))
>>> print(y)
{
    a: ivy.array([[1, 2, 3],
                  [1, 2, 3],
                  [1, 2, 3]]),
    b: ivy.array([[4, 5, 6],
                  [4, 5, 6],
                  [4, 5, 6]])
}
ivy.can_cast(from_, to, /)[source]

Determines if one data type can be cast to another data type according to type-promotion rules.

Parameters
  • from – input data type or 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.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.data_type_functions.can_cast.html>`_ )

  • 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.Dtype input:

>>> print(ivy.can_cast(ivy.uint8, ivy.int32))
True
>>> print(ivy.can_cast(ivy.float64, 'int64'))
False

With ivy.Array input:

>>> x = ivy.array([1., 2., 3.])
>>> print(ivy.can_cast(x, ivy.float64))
True

With ivy.NativeArray input:

>>> x = ivy.native_array([[-1, -1, -1],                              [1, 1, 1]],                            dtype='int16')
>>> print(ivy.can_cast(x, 'uint8'))
False

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),                          b=ivy.array([3, 4, 5]))
>>> print(ivy.can_cast(x, 'int64'))
{
    a: false,
    b: true
}
ivy.closest_valid_dtype(type, /)[source]

Determines the closest valid datatype to the datatype passed as input.

Parameters

type (Optional[Union[Dtype, str]]) – The data type for which to check the closest valid type for.

Return type

Union[Dtype, str]

Returns

  • ret – The closest valid data type as a native ivy.Dtype

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.elementwise_functions.add.html>`_ )

  • in the standard.

Examples

With ivy.Dtype input:

>>> xType = ivy.float16
>>> yType = ivy.closest_valid_dtype(xType)
>>> print(yType)
float16
>>> xType = ivy.int8
>>> yType = ivy.closest_valid_dtype(xType)
>>> print(yType)
int8

With ivy.Native_dtype inputs:

>>> xType = ivy.native_uint16
>>> yType = ivy.closest_valid_dtype(xType)
>>> print(yType)
<dtype:'uint16'>

With str input:

>>> xType = 'int32'
>>> yType = ivy.closest_valid_dtype(xType)
>>> print(yType)
int32
ivy.default_dtype(*, dtype=None, item=None, as_native=None)[source]

Summary.

Parameters
  • dtype (Optional[Union[Dtype, str]]) – (default: None)

  • item – (Default value = None)

  • as_native (Optional[bool]) – (Default value = None) (default: None)

Return type

Union[Dtype, str]

Returns

Return the input dtype if provided, otherwise return the global default dtype.

ivy.default_float_dtype(*, input=None, float_dtype=None, as_native=None)[source]

Summary.

Parameters
  • input – (Default value = None)

  • float_dtype (Optional[Union[FloatDtype, NativeDtype]]) – (default: None)

  • as_native (Optional[bool]) – (Default value = None) (default: None)

Return type

Union[Dtype, str, NativeDtype]

Returns

  • Return the input float dtype if provided, otherwise return the global default

  • float dtype.

ivy.default_int_dtype(*, input=None, int_dtype=None, as_native=False)[source]

Summary.

Parameters
  • input – (Default value = None)

  • int_dtype (Optional[Union[IntDtype, NativeDtype]]) – (default: None)

  • as_native (Optional[bool]) – (Default value = None) (default: False)

Return type

Union[IntDtype, NativeDtype]

Returns

  • Return the input int dtype if provided, otherwise return the global default int

  • dtype.

ivy.default_uint_dtype(*, input=None, uint_dtype=None, as_native=None)[source]

Returns the default uint dtype currently set. If input number or array is given, returns uint dtype according to input, else uint32 by default.

Parameters
  • input – Number or array for inferring default uint dtype. Optional.

  • uint_dtype (Optional[Union[UintDtype, NativeDtype]]) – Uint dtype to be returned as defualt. Optional. (default: None)

  • as_native (Optional[bool]) – Whether to return the default uint dtype as native dtype. Optional. (default: None)

Return type

Union[UintDtype, NativeDtype]

Returns

  • Return the input uint dtype if provided, otherwise return the global default

  • uint dtype.

Examples

>>> ivy.set_default_uint_dtype(ivy.UintDtype("uint16"))
>>> ivy.default_uint_dtype()
'uint16'
>>> ivy.default_uint_dtype(input=4294967346)
'uint64'
>>> ivy.default_uint_dtype(uint_dtype=ivy.UintDtype("uint8"))
'uint8'
>>> x = ivy.array([9,8], dtype="uint32")
>>> ivy.default_uint_dtype(input=x)
'uint32'
ivy.dtype(x, *, as_native=False)[source]

Get the data type for input array x.

Parameters
  • x (Union[Array, NativeArray]) – Tensor for which to get the data type.

  • as_native (bool) – Whether or not to return the dtype in string format. Default is False. (default: False)

Return type

Union[Dtype, NativeDtype]

Returns

  • ret – Data type of the array

  • Functional Method Examples

  • ————————–

  • With ivy.Array inputs

  • >>> x1 = ivy.array([1, 0, 1, -1, 0])

  • >>> y = ivy.dtype(x1)

  • >>> print(y)

  • int32

  • >>> x1 = ivy.array([1.0, 2.0, 3.5, 4.5, 5, 6])

  • >>> y = ivy.dtype(x1)

  • >>> print(y)

  • float32

  • With ivy.Native_Array inputs

  • >>> x1 = ivy.native_array([1, 0, 1, -1, 0])

  • >>> y = ivy.dtype(x1)

  • >>> print(y)

  • int32

  • >>> x1 = ivy.native_array([1.0, 2.0, 3.5, 4.5, 5, 6])

  • >>> y = ivy.dtype(x1)

  • >>> print(y)

  • float32

  • With ivy.Container inputs

  • >>> x = ivy.Container(a=ivy.array([1, 0, -1, 0, 1]), b=ivy.array([1, 0, -1, 0, 1]))

  • >>> y = ivy.dtype(x.a)

  • >>> print(y)

  • int32

  • >>> x = ivy.Container(a=ivy.native_array([1.0, 2.0, -1.0, 4.0, 1.0]), b=ivy.native_array([1, 0, 0, 0, 1]))

  • >>> y = ivy.dtype(x.a)

  • >>> print(y)

  • float32

Instance Method Examples

With ivy.Array inputs:

>>> x = ivy.array([1, 2, 3])
>>> y = x.dtype
>>> print(y)
int32

With ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([1, 2, 3]),                      b=ivy.array([2, 3, 4]))
>>> y = x.dtype()
>>> print(y)
{
    a: int32,
    b: int32
}
ivy.dtype_bits(dtype_in, /)[source]

Get the number of bits used for representing the input data type.

Parameters

dtype_in (Union[Dtype, NativeDtype, str]) – The data type to determine the number of bits for.

Return type

int

Returns

ret – The number of bits used to represent the data type.

Examples

With ivy.Dtype inputs:

>>> x = ivy.dtype_bits(ivy.float32)
>>> print(x)
32
>>> x = ivy.dtype_bits('int64')
>>> print(x)
64
>>> x = ivy.dtype_bits(ivy.uint16)
>>> print(x)
16

With ivy.NativeDtype inputs:

>>> x = ivy.dtype_bits(ivy.native_int8)
>>> print(x)
8
>>> x = ivy.dtype_bits(ivy.native_bool)
>>> print(x)
1
ivy.finfo(type, /)[source]

Machine limits for floating-point data types.

Parameters

type (Union[Dtype, str, Array, NativeArray]) – the kind of floating-point data-type about which to get information.

Return type

None

Returns

  • ret – an object having the followng attributes:

    • bits: int

      number of bits occupied by the floating-point data type.

    • eps: float

      difference between 1.0 and the next smallest representable floating-point number larger than 1.0 according to the IEEE-754 standard.

    • max: float

      largest representable number.

    • min: float

      smallest representable number.

    • smallest_normal: float

      smallest positive floating-point number with full precision.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.data_type_functions.can_cast.html>`_ )

  • in the standard.

Examples

With ivy.Dtype input:

>>> ivy.finfo(ivy.float32)
finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)

With str input:

>>> ivy.finfo('float32')
finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)

With ivy.Array input:

>>> x = ivy.array([1.3,2.1,3.4], dtype=ivy.float64)
>>> ivy.finfo(x)
finfo(resolution=1e-15, min=-1.7976931348623157e+308, /
max=1.7976931348623157e+308, dtype=float64)

With ivy.NativeArray input:

>>> x = ivy.native_array([0.7,8.4,3.14], dtype=ivy.float16)
>>> ivy.finfo(x)
finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16)

With ivy.Container input:

>>> c = ivy.Container(x=ivy.array([-9.5,1.8,-8.9], dtype=ivy.float16), /
                      y=ivy.array([7.6,8.1,1.6], dtype=ivy.float64))
>>> ivy.finfo(c)
{
    x: finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16),
    y: finfo(resolution=1e-15, min=-1.7976931348623157e+308, /
       max=1.7976931348623157e+308, dtype=float64)
}

Using ivy.Array instance method:

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

Using ivy.Container instance method:

>>> c = ivy.Container(x=ivy.array([1.2,3.5,8.], dtype=ivy.float64), /
                      y=ivy.array([1.3,2.1,3.4], dtype=ivy.float16))
>>> c.finfo()
{
    x: finfo(resolution=1e-15, min=-1.7976931348623157e+308, /
             max=1.7976931348623157e+308, dtype=float64)
    y: finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16),
}
ivy.function_supported_dtypes(fn)[source]

Returns the supported data types of the current backend’s function.

Parameters

fn (Callable) – The function to check for the supported dtype attribute

Return type

Tuple

Returns

ret – The supported data types of the function

Examples

>>> ivy.set_backend('torch')
>>> print(ivy.function_supported_dtypes(ivy.acosh))
()
ivy.function_unsupported_dtypes(fn)[source]

Returns the unsupported data types of the current backend’s function.

Parameters

fn (Callable) – The function to check for the unsupported dtype attribute

Return type

Tuple

Returns

ret – The unsupported data types of the function

Examples

>>> ivy.set_backend('torch')
>>> print(ivy.function_unsupported_dtypes(ivy.acosh))
('float16','uint16','uint32','uint64')
ivy.iinfo(type, /)[source]

Machine limits for integer data types.

Parameters

type (Union[Dtype, str, Array, NativeArray]) – the kind of integer data-type about which to get information.

Return type

None

Returns

  • ret – a class with that encapsules the following attributes:

    • bits: int

      number of bits occupied by the type.

    • max: int

      largest representable number.

    • min: int

      smallest representable number.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.data_type_functions.iinfo.html>`_ )

  • in the standard.

Examples

With ivy.Dtype input:

>>> ivy.iinfo(ivy.int32)
iinfo(min=-2147483648, max=2147483647, dtype=int32)
>>> ivy.iinfo(ivy.uint64)
iinfo(min=0, max=18446744073709551615, dtype=uint64)

With str input:

>>> ivy.iinfo('int32')
iinfo(min=-2147483648, max=2147483647, dtype=int32)
>>> ivy.iinfo('int64')
iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

With ivy.Array input:

>>> x = ivy.array([13,21,34])
>>> ivy.iinfo(x)
iinfo(min=-2147483648, max=2147483647, dtype=int32)
>>> x = ivy.array([13,21,34], dtype=ivy.int8)
>>> ivy.iinfo(x)
iinfo(min=-128, max=127, dtype=int8)

With ivy.NativeArray input:

>>> x = ivy.native_array([7,84,314], dtype=ivy.int16)
>>> ivy.iinfo(x)
iinfo(min=-32768, max=32767, dtype=int16)
>>> x = ivy.native_array([7,84,314], dtype=ivy.int64)
>>> ivy.iinfo(x)
iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

With ivy.Container input:

>>> c = ivy.Container(x=ivy.array([-9,1800,89], dtype=ivy.int16),                           y=ivy.array([76,-81,16], dtype=ivy.int32))
>>> ivy.iinfo(c)
{
    x: iinfo(min=-32768, max=32767, dtype=int16),
    y: iinfo(min=-2147483648, max=2147483647, dtype=int32)
}
>>> c = ivy.Container(x=ivy.array([0,1800,89], dtype=ivy.uint16),                           y=ivy.array([76,81,16], dtype=ivy.uint32))
>>> ivy.iinfo(c)
{
    x: iinfo(min=0, max=65535, dtype=uint16),
    y: iinfo(min=0, max=4294967295, dtype=uint32)
}

Using ivy.Array instance method:

>>> x = ivy.array([109,8400,14], dtype=ivy.int32)
>>> x.iinfo()
iinfo(min=-2147483648, max=2147483647, dtype=int32)
>>> x = ivy.array([-119,122,14], dtype=ivy.int8))
>>> x.iinfo()
iinfo(min=-128, max=127, dtype=int8)

Using ivy.Container instance method:

>>> c = ivy.Container(x=ivy.array([-9,1800,89], dtype=ivy.int16),                           y=ivy.array([76,-81,16], dtype=ivy.int32))
>>> c.iinfo()
{
    x: iinfo(min=-32768, max=32767, dtype=int16),
    y: iinfo(min=-2147483648, max=2147483647, dtype=int32)
}
ivy.invalid_dtype(dtype_in, /)[source]

Determines whether the provided data type is not support by the current framework.

Parameters

dtype_in (Optional[Union[Dtype, str]]) – The data type for which to check for backend non-support

Return type

bool

Returns

ret – Boolean, whether the data-type string is un-supported.

Examples

>>> ivy.invalid_dtype(dtype_in = None)
False
>>> ivy.invalid_dtype(dtype_in = 'uint64')
True
>>> ivy.invalid_dtype(dtype_in = ivy.float64)
True
>>> ivy.invalid_dtype(dtype_in = 'float32')
True
>>> ivy.invalid_dtype(dtype_in = ivy.native_int16)
True
>>> ivy.invalid_dtype(dtype_in = 'native_int16')
True
ivy.is_bool_dtype(dtype_in, /)[source]

Determine whether the input data type is a bool data type.

Parameters

dtype_in (Union[Dtype, str, Array, NativeArray, Number]) – input data type to test.

Return type

bool

Returns

  • ret – “True” if the input data type is a bool, otherwise “False”.

  • 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.

ivy.is_float_dtype(dtype_in, /)[source]

Determine whether the input data type is a float dtype.

Parameters

dtype_in (Union[Dtype, str, Array, NativeArray, Number]) – The array or data type to check

Return type

bool

Returns

ret – Whether or not the array or data type is of a floating point dtype

ivy.is_int_dtype(dtype_in, /)[source]

Determine whether the input data type is an int data type.

Parameters

dtype_in (Union[Dtype, str, Array, NativeArray, Number]) – input data type to test.

Return type

bool

Returns

  • ret – “True” if the input data type is an integer, otherwise “False”.

  • 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.Dtype input:

>>> x = ivy.is_int_dtype(ivy.int8)
>>> print(x)
True
>>> x = ivy.is_int_dtype(ivy.int32)
>>> print(x)
True
>>> x = ivy.is_int_dtype(ivy.float64)
>>> print(x)
False
>>> x = ivy.is_int_dtype(ivy.bool)
>>> print(x)
False

With ivy.Array input:

>>> x = ivy.array([1., 2., 3.])
>>> print(x.dtype)
float32
>>> print(ivy.is_int_dtype(x))
False

With ivy.NativeArray input:

>>> x = ivy.native_array([[-1, -1, -1], [1, 1, 1]],         dtype = ivy.int16)
>>> print(x.dtype)
torch.int16
>>> print(ivy.is_int_dtype(x))
True

With Number input:

>>> x = 1
>>> print(ivy.is_int_dtype(x))
True

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),         b=ivy.array([3, 4, 5]))
>>> print(x.a.dtype, x.b.dtype)
float32 int32
>>> print(ivy.is_int_dtype(x))
{
    a: false,
    b: true
}
ivy.is_uint_dtype(dtype_in, /)[source]

Determine whether the input data type is a uint dtype.

Parameters

dtype_in (Union[Dtype, str, Array, NativeArray, Number]) – The array or data type to check

Return type

bool

Returns

ret – Whether or not the array or data type is of a uint dtype

Examples

>>> ivy.is_uint_dtype(ivy.UintDtype("uint16"))
True
>>> ivy.is_uint_dtype(ivy.Dtype("uint8"))
True
>>> ivy.is_uint_dtype(ivy.IntDtype("int64"))
False
ivy.promote_types(type1, type2, /)[source]

Promotes the datatypes type1 and type2, returning the data type they promote to

Parameters
  • type1 (Union[Dtype, NativeDtype]) – the first of the two types to promote

  • type2 (Union[Dtype, NativeDtype]) – the second of the two types to promote

Return type

Dtype

Returns

ret – The type that both input types promote to

ivy.promote_types_of_inputs(x1, x2, /)[source]

Promotes the dtype of the given native array inputs to a common dtype based on type promotion rules. While passing float or integer values or any other non-array input to this function, it should be noted that the return will be an array-like object. Therefore, outputs from this function should be used as inputs only for those functions that expect an array-like or tensor-like objects, otherwise it might give unexpected results.

Return type

Tuple[NativeArray, NativeArray]

ivy.result_type(*arrays_and_dtypes)[source]

Returns the dtype that results from applying the type promotion rules (see type-promotion) to the arguments.

Note

If provided mixed dtypes (e.g., integer and floating-point), the returned dtype will be implementation-specific.

Parameters

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.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.data_type_functions.result_type.html>`_ )

  • in the standard.

Examples

With ivy.Array input:

>>> x = ivy.array([3, 4, 5])
>>> y = ivy.array([3., 4., 5.])
>>> print(ivy.result_type(x, y))
float64

With ivy.NativeArray input:

>>> x = ivy.native_array([3., 4, 7.5])
>>> y = ivy.native_array([3, 4, 7])
>>> print(ivy.result_type(x, y))
float64

With ivy.Dtype input:

>>> print(ivy.result_type(ivy.uint8, ivy.uint64))
uint64

With ivy.Container input:

>>> x = ivy.Container(a = ivy.array([3, 4, 5]))
>>> print(x.a.dtype)
int32
>>> print(ivy.result_type(x, ivy.float64))
{
    a: float64
}
ivy.set_default_dtype(dtype, /)[source]

Sets the datatype dtype as default data type

Parameters

dtype (Union[Dtype, NativeDtype, str]) – the data_type to set as default data type

Examples

With ivy.Dtype input:

>>> ivy.set_default_dtype("float64")
>>> ivy.default_dtype_stack
    ['float64']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype(ivy.bool)
>>> ivy.default_dtype_stack
    ['bool']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype(ivy.int32)
>>> ivy.default_dtype_stack
    ['int32']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype('uint8')
>>> ivy.default_dtype_stack
    ['uint8']
>>> ivy.unset_default_dtype()

With ivy.NativeDtype input:

>>> ivy.set_default_dtype(ivy.native_int32)
>>> ivy.default_dtype_stack
    ['int32']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype('native_bool')
>>> ivy.default_dtype_stack
    ['native_bool']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype(ivy.native_uint64)
>>> ivy.default_dtype_stack
    ['uint64']
>>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype('native_float64')
>>> ivy.default_dtype_stack
    ['native_float64']
>>> ivy.unset_default_dtype()
ivy.set_default_float_dtype(float_dtype, /)[source]

Summary.

Parameters

float_dtype (Union[Dtype, str]) –

ivy.set_default_int_dtype(int_dtype, /)[source]

Summary.

Parameters

int_dtype (Union[Dtype, str]) –

ivy.set_default_uint_dtype(uint_dtype, /)[source]

Set the uint dtype to be default.

Parameters

uint_dtype (Union[Dtype, str]) – The uint dtype to be set as default.

Examples

>>> ivy.set_default_uint_dtype(ivy.UintDtype("uint8"))
>>> ivy.default_uint_dtype()
'uint8'
>>> ivy.set_default_uint_dtype(ivy.UintDtype("uint64"))
>>> ivy.default_uint_dtype()
'uint64'
ivy.type_promote_arrays(x1, x2, /)[source]

Type promote the input arrays, returning new arrays with the shared correct data type

Parameters
  • x1 (Union[Array, NativeArray]) – the first of the two arrays to type promote

  • x2 (Union[Array, NativeArray]) – the second of the two arrays to type promote

Return type

Tuple

Returns

ret1, ret2 – The input arrays after type promotion

ivy.unset_default_dtype()[source]
ivy.unset_default_float_dtype()[source]
ivy.unset_default_int_dtype()[source]
ivy.unset_default_uint_dtype()[source]

Reset the current default uint dtype to the previous state

Examples

>>> ivy.set_default_uint_dtype(ivy.UintDtype("uint8"))
>>> ivy.default_uint_dtype()
'uint8'
>>> ivy.unset_default_uint_dtype()
>>> ivy.default_uint_dtype()
'uint32'
ivy.valid_dtype(dtype_in, /)[source]

Determines whether the provided data type is supported by the current framework.

Parameters

dtype_in (Optional[Union[Dtype, NativeDtype, str]]) – The data type for which to check for backend support

Return type

bool

Returns

ret – Boolean, whether or not the data-type string is supported.

Examples

with ivy.Dtype inputs:

>>> print(ivy.valid_dtype(None))
True
>>> print(ivy.valid_dtype('float16'))
True
>>> print(ivy.valid_dtype('float32'))
True
>>> print(ivy.valid_dtype(ivy.float64))
True
>>> print(ivy.valid_dtype('bool'))
True
>>> print(ivy.valid_dtype(ivy.int8))
True
>>> print(ivy.valid_dtype(ivy.int64))
True
>>> print(ivy.valid_dtype(ivy.uint8))
True

with ivy.NativeDtype inputs:

>>> print(ivy.valid_dtype('native_bool'))
False
>>> print(ivy.valid_dtype(ivy.native_float16))
True
>>> print(ivy.valid_dtype(ivy.native_float32))
True
>>> print(ivy.valid_dtype('native_float64'))
False
>>> print(ivy.valid_dtype(ivy.native_int8))
True
>>> print(ivy.valid_dtype(ivy.native_int16))
True
>>> print(ivy.valid_dtype('native_int32'))
False
>>> print(ivy.valid_dtype(ivy.native_int64))
True
>>> print(ivy.valid_dtype(ivy.native_uint8))
True
>>> print(ivy.valid_dtype('native_uint64'))
False