jax.numpy.can_cast#

jax.numpy.can_cast(from_, to, casting='safe')#

如果可以根据 casting 规则在数据类型之间进行转换,则返回 True。

参数:
  • from (dtype, dtype specifier, NumPy 标量, 或 数组) – 要从中转换的数据类型、NumPy 标量或数组。

  • to (dtypedtype specifier) – 要转换到的数据类型。

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, 可选) –

    控制可能发生的数据类型转换的种类。

    • “no” 表示数据类型根本不应转换。

    • “equiv” 表示仅允许字节顺序更改。

    • “safe” 表示仅允许可以保留值的转换。

    • “same_kind” 表示仅允许安全转换或种类内的转换,例如 float64 到 float32。

    • “unsafe” 表示可以进行任何数据转换。

返回:

out – 如果可以根据 casting 规则进行转换,则为 True。

返回类型:

bool

Notes

在版本 2.0 中更改: 此函数不再支持 Python 标量,并且不适用于 0-D 数组和 NumPy 标量的任何基于值的逻辑。

参见

dtype, result_type

示例

基本示例

>>> import numpy as np
>>> np.can_cast(np.int32, np.int64)
True
>>> np.can_cast(np.float64, complex)
True
>>> np.can_cast(complex, float)
False
>>> np.can_cast('i8', 'f8')
True
>>> np.can_cast('i8', 'f4')
False
>>> np.can_cast('i4', 'S4')
False