Skip to main content
`Ctrl`+`K`

# 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`, and `complex` thus behave weak and should be passed for “untyped” Python input.

  Parameters:
    dtypes tuple 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.

    signature tuple 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.

    reduction boolean

      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:
    dtypes tuple of dtypes

      The dtypes which NumPy would use for the calculation. Note that dtypes may not match the passed in ones (casting is necessary).

  Examples

  This API requires passing dtypes, define them for convenience:

      >>> import numpy as np
      >>> int32 = np.dtype("int32")
      >>> float32 = np.dtype("float32")

  The typical ufunc call does not pass an output dtype. `numpy.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 passing `int`, `float`, or `complex`:

      >>> np.add.resolve_dtypes((float32, float, None))
      (dtype('float32'), dtype('float32'), dtype('float32'))

  Where the Python `float` behaves similar to a Python value `0.0` in a ufunc call. (See NEP 50 for details.)