fn

result_type

DType
result_type(a: Tensor | Scalar, b: Tensor | Scalar)
source

Compute the dtype that a binary operation on the inputs would produce.

Implements NumPy-style type promotion: tensors contribute their declared dtype, while Python scalars are treated as having no dtype of their own and follow whichever tensor operand they appear with.

Parameters

aTensor | Scalar
First operand. Tensor operands contribute their dtype; Python scalars do not.
bTensor | Scalar
Second operand. Same convention as a.

Returns

DType

The dtype that an arithmetic operation on a and b would produce.

Notes

The promotion algorithm groups dtypes into four kinds — bool <int<float<complex< \text{int} < \text{float} < \text{complex} — and uses bit-width as a tiebreaker. Concretely:

result_type(a,b)={dtype(a),b is scalar,dtype(b),a is scalar,float32,both scalar,promote(dtype(a),dtype(b)),otherwise.\text{result\_type}(a, b) = \begin{cases} \text{dtype}(a), & b\ \text{is scalar}, \\ \text{dtype}(b), & a\ \text{is scalar}, \\ \text{float32}, & \text{both scalar}, \\ \operatorname{promote}(\text{dtype}(a), \text{dtype}(b)), & \text{otherwise}. \end{cases}

See also promote_types (operates directly on dtype objects).

Examples

>>> import lucid
>>> a = lucid.tensor([1, 2], dtype=lucid.int32)
>>> b = lucid.tensor([1., 2.], dtype=lucid.float32)
>>> lucid.result_type(a, b)
lucid.float32