jax.numpy.can_cast#
- jax.numpy.can_cast(from_, to, casting='safe')#
如果可以根據 casting 規則進行資料類型之間的轉換,則傳回 True。
- 參數:
from (dtype、 dtype 指定符、 NumPy 純量或 陣列) – 要從其轉換的資料類型、NumPy 純量或陣列。
to (dtype 或 dtype 指定符) – 要轉換成的資料類型。
casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, 選用) –
控制可能發生的資料 casting 類型。
’no’ 表示不應轉換資料類型。
’equiv’ 表示僅允許位元組順序變更。
’safe’ 表示僅允許可以保留值的轉換。
’same_kind’ 表示僅允許安全轉換或種類內的轉換,例如 float64 到 float32。
’unsafe’ 表示可以進行任何資料轉換。
- 傳回:
out – 如果可以根據 casting 規則進行轉換,則為 True。
- 傳回類型:
注意事項
在版本 2.0 中變更:此函式不再支援 Python 純量,並且不對 0-D 陣列和 NumPy 純量應用任何基於價值的邏輯。
參見
範例
基本範例
>>> 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