result_type(*arrays_and_dtypes)
Type promotion in NumPy works similarly to the rules in languages like C++, with some slight differences. When both scalars and arrays are used, the array's type takes precedence and the actual value of the scalar is taken into account.
For example, calculating 3*a, where a is an array of 32-bit floats, intuitively should result in a 32-bit float output. If the 3 is a 32-bit integer, the NumPy rules indicate it can't convert losslessly into a 32-bit float, so a 64-bit float should be the result type. By examining the value of the constant, '3', we see that it fits in an 8-bit integer, which can be cast losslessly into the 32-bit float.
The specific algorithm used is as follows.
Categories are determined by first checking which of boolean, integer (int/uint), or floating point (float/complex) the maximum kind of all the arrays and the scalars are.
If there are only scalars or the maximum category of the scalars is higher than the maximum category of the arrays, the data types are combined with promote_types
to produce the return value.
Otherwise, min_scalar_type
is called on each array, and the resulting data types are all combined with promote_types
to produce the return value.
The set of int values is not a subset of the uint values for types with the same number of bits, something not reflected in min_scalar_type
, but handled as a special case in result_type
.
The operands of some operation whose result type is needed.
The result type.
Returns the type that results from applying the NumPy type promotion rules to the arguments.
>>> np.result_type(3, np.arange(7, dtype='i1')) dtype('int8')
>>> np.result_type('i4', 'c8') dtype('complex128')
>>> np.result_type(3.0, -2) dtype('float64')See :
The following pages refer to to this document either explicitly or contain code examples using this.
numpy.result_type
numpy.min_scalar_type
numpy.dtype
numpy.core._multiarray_umath.min_scalar_type
numpy.promote_types
dask.array.routines.average
numpy.average
numpy.core._multiarray_umath.can_cast
numpy.can_cast
numpy.core._multiarray_umath.result_type
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them