numpy.ufunc.resolve_dtypes#
method
- ufunc.resolve_dtypes(dtypes, *, signature=None, casting=None, reduction=False)#
Find the dtypes NumPy will use for the operation. Both input and output dtypes are returned and may differ from those provided.
Note
This function always applies NEP 50 rules since it is not provided any actual values. The Python types
int
,float
, andcomplex
thus behave weak and should be passed for “untyped” Python input.- Parameters:
- dtypestuple of dtypes, None, or literal int, float, complex
The input dtypes for each operand. Output operands can be None, indicating that the dtype must be found.
- signaturetuple of DTypes or None, optional
If given, enforces exact DType (classes) of the specific operand. The ufunc
dtype
argument is equivalent to passing a tuple with only output dtypes set.- casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional
The casting mode when casting is necessary. This is identical to the ufunc call casting modes.
- reductionboolean
If given, the resolution assumes a reduce operation is happening which slightly changes the promotion and type resolution rules.
dtypes
is usually something like(None, np.dtype("i2"), None)
for reductions (first input is also the output).Note
The default casting mode is “same_kind”, however, as of NumPy 1.24, NumPy uses “unsafe” for reductions.
- Returns:
- dtypestuple of dtypes
The dtypes which NumPy would use for the calculation. Note that dtypes may not match the passed in ones (casting is necessary).
See also
numpy.ufunc._resolve_dtypes_and_context
Similar function to this, but returns additional information which give access to the core C functionality of NumPy.
Examples
This API requires passing dtypes, define them for convenience:
>>> int32 = np.dtype("int32") >>> float32 = np.dtype("float32")
The typical ufunc call does not pass an output dtype. np.add has two inputs and one output, so leave the output as
None
(not provided):>>> np.add.resolve_dtypes((int32, float32, None)) (dtype('float64'), dtype('float64'), dtype('float64'))
The loop found uses “float64” for all operands (including the output), the first input would be cast.
resolve_dtypes
supports “weak” handling for Python scalars by passingint
,float
, orcomplex
:>>> np.add.resolve_dtypes((float32, float, None)) (dtype('float32'), dtype('float32'), dtype('float32'))
Where the Python
float
behaves samilar to a Python value0.0
in a ufunc call. (See NEP 50 for details.)