Data type#
- 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]#
Copy an array to a specified data type irrespective of type- promotion rules.
Casting floating-point
NaN
andinfinity
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 to1
, and a value ofFalse
must cast to a numeric value equal to0
.When casting a numeric input array to
bool
, a value of0
must cast toFalse
, and a non-zero value must cast toTrue
.- Parameters:
x (
Union
[Array
,NativeArray
]) – array to cast.dtype (
Union
[Dtype
,NativeDtype
]) – desired data type.copy (
bool
, default:True
) – specifies whether to copy an array when the specifieddtype
matches the data type of the input arrayx
. IfTrue
, a newly allocated array must always be returned. IfFalse
and the specifieddtype
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:
- Returns:
ret – an array having the specified data type. The returned array must have the same shape as
x
.
Examples
With
ivy.Array
input:>>> x = ivy.array([1, 2]) >>> y = ivy.zeros_like(x) >>> y = ivy.astype(x, ivy.float64) >>> print(y) ivy.array([1., 2.])
>>> x = ivy.array([3.141, 2.718, 1.618]) >>> y = ivy.zeros_like(x) >>> ivy.astype(x, ivy.int32, out=y) >>> print(y) ivy.array([3., 2., 1.])
>>> x = ivy.array([[-1, -2], [0, 2]]) >>> ivy.astype(x, ivy.float64, out=x) >>> print(x) ivy.array([[-1., -2.], [0., 2.]])
With
ivy.NativeArray
input:>>> x = ivy.native_array([3.141, 2.718, 1.618]) >>> y = ivy.astype(x, ivy.int32) >>> print(y) ivy.array([3, 2, 1])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([0,2,1]),b=ivy.array([1,0,0])) >>> print(ivy.astype(x, ivy.bool)) { a: ivy.array([False, True, True]), b: ivy.array([True, False, False]) }
With
ivy.Array
instance method:>>> x = ivy.array([[-1, -2], [0, 2]]) >>> print(x.astype(ivy.float64)) ivy.array([[-1., -2.], [0., 2.]])
With
ivy.Container
instance method:>>> x = ivy.Container(a=ivy.array([False,True,True]), ... b=ivy.array([3.14, 2.718, 1.618])) >>> print(x.astype(ivy.int32)) { a: ivy.array([0, 1, 1]), b: ivy.array([3, 2, 1]) }
- 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.- Return type:
List
[Array
]- Returns:
ret – A list containing broadcasted arrays of type ivy.Array Each array must have the same shape, and each array must have the same dtype as its corresponding input 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
andivy.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
andivy.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
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- 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]#
Determine 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:
ret –
True
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/array_api.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.check_float(x)[source]#
Check if the input is a float or a float-like object.
- Parameters:
x (
Any
) – Input to check.- Return type:
bool
- Returns:
ret – “True” if the input is a float or a float-like object, otherwise “False”.
- ivy.closest_valid_dtype(type, /)[source]#
Determine 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
Examples
With
ivy.Dtype
input:>>> xType = ivy.float16 >>> yType = ivy.closest_valid_dtype(xType) >>> print(yType) float16
With
ivy.NativeDtype
inputs:>>> xType = ivy.native_uint16 >>> yType = ivy.closest_valid_dtype(xType) >>> print(yType) uint16
With
str
input:>>> xType = 'int32' >>> yType = ivy.closest_valid_dtype(xType) >>> print(yType) int32
- ivy.default_complex_dtype(*, input=None, complex_dtype=None, as_native=False)[source]#
- Parameters:
input (
Optional
[Union
[Array
,NativeArray
]], default:None
) – Number or array for inferring the complex dtype.complex_dtype (
Optional
[Union
[ComplexDtype
,NativeDtype
]], default:None
) – The complex dtype to be returned.as_native (
bool
, default:False
) – Whether to return the complex dtype as native dtype.
- Return type:
Union
[Dtype
,str
,NativeDtype
]- Returns:
Return
complex_dtype
as native or ivy dtype if provided, else ifinput
is given, return its complex dtype, otherwise return the global default complex dtype.
Examples
>>> ivy.default_complex_dtype() 'complex64'
>>> ivy.set_default_complex_dtype(ivy.ComplexDtype("complex64")) >>> ivy.default_complex_dtype() 'complex64'
>>> ivy.default_complex_dtype(complex_dtype=ivy.ComplexDtype("complex128")) 'complex128'
>>> ivy.default_complex_dtype(input=4294.967346) 'complex64'
>>> x = ivy.array([9.8,8.9], dtype="complex128") >>> ivy.default_complex_dtype(input=x) 'complex128'
- ivy.default_dtype(*, dtype=None, item=None, as_native=False)[source]#
- Parameters:
item (
Optional
[Union
[Array
,NativeArray
]], default:None
) – Number or array for inferring the dtype.dtype (
Optional
[Union
[Dtype
,str
]], default:None
) – The dtype to be returned.as_native (
bool
, default:False
) – Whether to return the dtype as native dtype.
- Return type:
Union
[Dtype
,NativeDtype
,str
]- Returns:
Return
dtype
as native or ivy dtype if provided, else ifitem
is given, return its dtype, otherwise return the global default dtype.
Examples
>>> ivy.default_dtype() 'float32'
>>> ivy.set_default_dtype(ivy.bool) >>> ivy.default_dtype() 'bool'
>>> ivy.set_default_dtype(ivy.int16) >>> ivy.default_dtype() 'int16'
>>> ivy.set_default_dtype(ivy.float64) >>> ivy.default_dtype() 'float64'
>>> ivy.default_dtype(dtype="int32") 'int32'
>>> ivy.default_dtype(dtype=ivy.float16) 'float16'
>>> ivy.default_dtype(item=53.234) 'float64'
>>> ivy.default_dtype(item=[1, 2, 3]) 'int32'
>>> x = ivy.array([5.2, 9.7], dtype="complex128") >>> ivy.default_dtype(item=x) 'complex128'
- ivy.default_float_dtype(*, input=None, float_dtype=None, as_native=False)[source]#
- Parameters:
input (
Optional
[Union
[Array
,NativeArray
]], default:None
) – Number or array for inferring the float dtype.float_dtype (
Optional
[Union
[FloatDtype
,NativeDtype
]], default:None
) – The float dtype to be returned.as_native (
bool
, default:False
) – Whether to return the float dtype as native dtype.
- Return type:
Union
[Dtype
,str
,NativeDtype
]- Returns:
Return
float_dtype
as native or ivy dtype if provided, else ifinput
is given, return its float dtype, otherwise return the global default float dtype.
Examples
>>> ivy.default_float_dtype() 'float32'
>>> ivy.set_default_float_dtype(ivy.FloatDtype("float64")) >>> ivy.default_float_dtype() 'float64'
>>> ivy.default_float_dtype(float_dtype=ivy.FloatDtype("float16")) 'float16'
>>> ivy.default_float_dtype(input=4294.967346) 'float32'
>>> x = ivy.array([9.8,8.9], dtype="float16") >>> ivy.default_float_dtype(input=x) 'float16'
- ivy.default_int_dtype(*, input=None, int_dtype=None, as_native=False)[source]#
- Parameters:
input (
Optional
[Union
[Array
,NativeArray
]], default:None
) – Number or array for inferring the int dtype.int_dtype (
Optional
[Union
[IntDtype
,NativeDtype
]], default:None
) – The int dtype to be returned.as_native (
bool
, default:False
) – Whether to return the int dtype as native dtype.
- Return type:
Union
[IntDtype
,NativeDtype
]- Returns:
Return
int_dtype
as native or ivy dtype if provided, else ifinput
is given, return its int dtype, otherwise return the global default int dtype.
Examples
>>> ivy.set_default_int_dtype(ivy.intDtype("int16")) >>> ivy.default_int_dtype() 'int16'
>>> ivy.default_int_dtype(input=4294967346) 'int64'
>>> ivy.default_int_dtype(int_dtype=ivy.intDtype("int8")) 'int8'
>>> x = ivy.array([9,8], dtype="int32") >>> ivy.default_int_dtype(input=x) 'int32'
- ivy.default_uint_dtype(*, input=None, uint_dtype=None, as_native=False)[source]#
- Parameters:
input (
Optional
[Union
[Array
,NativeArray
]], default:None
) – Number or array for inferring the uint dtype.uint_dtype (
Optional
[Union
[UintDtype
,NativeDtype
]], default:None
) – The uint dtype to be returned.as_native (
bool
, default:False
) – Whether to return the uint dtype as native dtype.
- Return type:
Union
[UintDtype
,NativeDtype
]- Returns:
Return
uint_dtype
as native or ivy dtype if provided, else ifinput
is given, return its uint dtype, 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
, default:False
) – Whether or not to return the dtype in string format. Default isFalse
.
- Return type:
Union
[Dtype
,NativeDtype
]- Returns:
ret – Data type of the array.
Examples
With
ivy.Array
inputs:>>> x1 = ivy.array([1.0, 2.0, 3.5, 4.5, 5, 6]) >>> y = ivy.dtype(x1) >>> print(y) float32
With
ivy.NativeArray
inputs:>>> x1 = ivy.native_array([1, 0, 1, -1, 0]) >>> y = ivy.dtype(x1) >>> print(y) int32
With
ivy.Container
inputs:>>> 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
- 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
With
ivy.NativeDtype
inputs:>>> 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 following 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. This docstring is an extension of the docstring in the standard.
Examples
With
ivy.Dtype
input:>>> y = ivy.finfo(ivy.float32) >>> print(y) finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
With
str
input:>>> y = ivy.finfo('float32') >>> print(y) 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) >>> print(ivy.finfo(x)) finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
>>> x = ivy.array([0.7,8.4,3.14], dtype=ivy.float16) >>> print(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)) >>> print(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) }
- ivy.function_supported_dtypes(fn, recurse=True)[source]#
Return the supported data types of the current backend’s function. The function returns a dict containing the supported dtypes for the compositional and primary implementations in case of partial mixed functions.
- Parameters:
fn (
Callable
) – The function to check for the supported dtype attributerecurse (
bool
, default:True
) – Whether to recurse into used ivy functions. Default isTrue
.
- Return type:
Union
[Tuple
,dict
]- Returns:
ret – Tuple or dict containing the supported dtypes of the function
Examples
>>> print(ivy.function_supported_dtypes(ivy.acosh)) ('bool', 'float64', 'int64', 'uint8', 'int8', 'float32', 'int32', 'int16', 'bfloat16')
- ivy.function_unsupported_dtypes(fn, recurse=True)[source]#
Return the unsupported data types of the current backend’s function. The function returns a dict containing the unsupported dtypes for the compositional and primary implementations in case of partial mixed functions.
- Parameters:
fn (
Callable
) – The function to check for the unsupported dtype attributerecurse (
bool
, default:True
) – Whether to recurse into used ivy functions. Default isTrue
.
- Return type:
Union
[Tuple
,dict
]- Returns:
ret – Tuple or dict containing the unsupported dtypes 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. This docstring is an extension of the docstring in the standard.
Examples
With
ivy.Dtype
input:>>> ivy.iinfo(ivy.int32) iinfo(min=-2147483648, max=2147483647, dtype=int32)
With
str
input:>>> ivy.iinfo('int32') iinfo(min=-2147483648, max=2147483647, dtype=int32)
With
ivy.Array
input:>>> 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.int64) >>> ivy.iinfo(x) iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
With
ivy.Container
input:>>> 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) }
- ivy.infer_default_dtype(dtype, as_native=False)[source]#
Summary.
- Parameters:
dtype (
Union
[Dtype
,NativeDtype
,str
]) –as_native (
bool
, default:False
) – (Default value = False)
- Return type:
Union
[Dtype
,NativeDtype
]- Returns:
Return the default data type for the “kind” (integer or floating-point) of dtype
Examples
>>> ivy.set_default_int_dtype("int32") >>> ivy.infer_default_dtype("int8") 'int8'
>>> ivy.set_default_float_dtype("float64") >>> ivy.infer_default_dtype("float32") 'float64'
>>> ivy.set_default_uint_dtype("uint32") >>> x = ivy.array([0], dtype="uint64") >>> ivy.infer_default_dtype(x.dtype) 'uint32'
- ivy.invalid_dtype(dtype_in, /)[source]#
Determine whether the provided data type is not support by the current framework.
- Parameters:
dtype_in (
Optional
[Union
[Dtype
,NativeDtype
,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
>>> print(ivy.invalid_dtype(None)) False
>>> print(ivy.invalid_dtype("uint64")) False
>>> print(ivy.invalid_dtype(ivy.float64)) False
>>> print(ivy.invalid_dtype(ivy.native_uint8)) False
- 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_complex_dtype(dtype_in, /)[source]#
Determine whether the input data type is a complex 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 complex dtype
Examples
>>> ivy.is_complex_dtype(ivy.ComplexDtype("complex64")) True
>>> ivy.is_complex_dtype(ivy.Dtype("complex128")) True
>>> ivy.is_complex_dtype(ivy.IntDtype("int64")) False
- 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
Examples
>>> x = ivy.is_float_dtype(ivy.float32) >>> print(x) True
>>> arr = ivy.array([1.2, 3.2, 4.3], dtype=ivy.float32) >>> print(ivy.is_float_dtype(arr)) True
- ivy.is_hashable_dtype(dtype_in, /)[source]#
Check if the given data type is hashable or not.
- Parameters:
dtype_in (
Union
[Dtype
,NativeDtype
]) – The data type to check.- Return type:
bool
- Returns:
ret – True if data type is hashable else False
- 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.float64) >>> print(x) False
With
ivy.Array
input:>>> x = ivy.array([1., 2., 3.]) >>> print(ivy.is_int_dtype(x), x.dtype) False float32
With
ivy.NativeArray
input:>>> x = ivy.native_array([[-1, -1, -1], [1, 1, 1]], dtype=ivy.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(ivy.is_int_dtype(x)) { a: False, b: True }
- ivy.is_native_dtype(dtype_in, /)[source]#
Determine whether the input dtype is a Native dtype.
- Parameters:
dtype_in (
Union
[Dtype
,NativeDtype
]) – Determine whether the input data type is a native data type object.- Return type:
bool
- Returns:
ret – Boolean, whether or not dtype_in is a native data type.
Examples
>>> ivy.set_backend('numpy') >>> ivy.is_native_dtype(np.int32) True
>>> ivy.set_backend('numpy') >>> ivy.is_native_array(ivy.float64) False
- 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, /, *, array_api_promotion=False)[source]#
Promote the datatypes type1 and type2, returning the data type they promote to.
- Parameters:
type1 (
Union
[Dtype
,NativeDtype
]) – the first of the two types to promotetype2 (
Union
[Dtype
,NativeDtype
]) – the second of the two types to promotearray_api_promotion (
bool
, default:False
) – whether to only use the array api promotion rules
- Return type:
Dtype
- Returns:
ret – The type that both input types promote to
- ivy.promote_types_of_inputs(x1, x2, /, *, array_api_promotion=False)[source]#
Promote 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]#
Return the dtype that results from applying the type promotion rules (see type-promotion) to the arguments.
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. This docstring is an extension of the docstring in the standard.
Examples
With
ivy.Array
input:>>> x = ivy.array([3, 4, 5]) >>> y = ivy.array([3., 4., 5.]) >>> d = ivy.result_type(x, y) >>> print(d) float32
With
ivy.Dtype
input:>>> d = ivy.result_type(ivy.uint8, ivy.uint64) >>> print(d) uint64
With
ivy.Container
input:>>> x = ivy.Container(a = ivy.array([3, 4, 5])) >>> d = x.a.dtype >>> print(d) int32
>>> x = ivy.Container(a = ivy.array([3, 4, 5])) >>> d = ivy.result_type(x, ivy.float64) >>> print(d) { a: float64 }
- ivy.set_default_complex_dtype(complex_dtype, /)[source]#
Set the ‘complex_dtype’ as the default data type.
- Parameters:
complex_dtype (
Union
[Dtype
,str
]) – The complex data type to be set as the default.
Examples
With :class: ivy.Dtype input:
>>> ivy.set_default_complex_dtype(ivy.ComplexDtype("complex64")) >>> ivy.default_complex_dtype() 'complex64'
>>> ivy.set_default_float_dtype(ivy.ComplexDtype("complex128")) >>> ivy.default_complex_dtype() 'complex128'
- ivy.set_default_dtype(dtype, /)[source]#
Set 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(ivy.bool) >>> ivy.default_dtype_stack ['bool'] >>> ivy.unset_default_dtype()
>>> ivy.set_default_dtype("float64") >>> ivy.default_dtype_stack ['float64'] >>> ivy.unset_default_dtype()
With
ivy.NativeDtype
input:>>> ivy.set_default_dtype(ivy.native_uint64) >>> ivy.default_dtype_stack ['uint64']
- ivy.set_default_float_dtype(float_dtype, /)[source]#
Set the ‘float_dtype’ as the default data type.
- Parameters:
float_dtype (
Union
[Dtype
,str
]) – The float data type to be set as the default.
Examples
With :class: ivy.Dtype input:
>>> ivy.set_default_float_dtype(ivy.floatDtype("float64")) >>> ivy.default_float_dtype() 'float64'
>>> ivy.set_default_float_dtype(ivy.floatDtype("float32")) >>> ivy.default_float_dtype() 'float32'
- ivy.set_default_int_dtype(int_dtype, /)[source]#
Set the ‘int_dtype’ as the default data type.
- Parameters:
int_dtype (
Union
[Dtype
,str
]) – The integer data type to be set as the default.
Examples
With :class: ivy.Dtype input:
>>> ivy.set_default_int_dtype(ivy.intDtype("int64")) >>> ivy.default_int_dtype() 'int64'
>>> ivy.set_default_int_dtype(ivy.intDtype("int32")) >>> ivy.default_int_dtype() 'int32'
- 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.
- ivy.unset_default_complex_dtype()[source]#
Reset the current default complex dtype to the previous state.
Examples
>>> ivy.set_default_complex_dtype(ivy.complex64) >>> ivy.set_default_complex_dtype(ivy.complex128) >>> ivy.default_complex_dtype_stack ['complex64','complex128']
>>> ivy.unset_default_complex_dtype() >>> ivy.default_complex_dtype_stack ['complex64']
- ivy.unset_default_dtype()[source]#
Reset the current default dtype to the previous state.
Examples
>>> ivy.set_default_dtype(ivy.int32) >>> ivy.set_default_dtype(ivy.bool) >>> ivy.default_dtype_stack ['int32', 'bool']
>>> ivy.unset_default_dtype() >>> ivy.default_dtype_stack ['int32']
>>> ivy.unset_default_dtype() >>> ivy.default_dtype_stack []
- ivy.unset_default_float_dtype()[source]#
Reset the current default float dtype to the previous state.
Examples
>>> ivy.set_default_float_dtype(ivy.float32) >>> ivy.set_default_float_dtype(ivy.float64) >>> ivy.default_float_dtype_stack ['float32','float64']
>>> ivy.unset_default_float_dtype() >>> ivy.default_float_dtype_stack ['float32']
- ivy.unset_default_int_dtype()[source]#
Reset the current default int dtype to the previous state.
Examples
>>> ivy.set_default_int_dtype(ivy.intDtype("int16")) >>> ivy.default_int_dtype() 'int16'
>>> ivy.unset_default_int_dtype() >>> ivy.default_int_dtype() 'int32'
- 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]#
Determine 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
>>> print(ivy.valid_dtype(None)) True
>>> print(ivy.valid_dtype(ivy.float64)) True
>>> print(ivy.valid_dtype('bool')) True
>>> print(ivy.valid_dtype(ivy.native_float16)) True
This should have hopefully given you an overview of the data_type submodule, if you have any questions, please feel free to reach out on our discord!