can_cast#

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:

  • 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/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
}
Array.can_cast(self, 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
Container.can_cast(self, to, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container 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 (Container) – input container from which to cast.

  • to (Union[Dtype, Container]) – desired data type.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

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

Examples

>>> 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(x.can_cast('int64'))
{
    a: False,
    b: True
}