@@ -81,13 +81,22 @@ Promotion always occurs along the green lines:
81
81
from left to right within their kind and to a higher kind only when
82
82
necessary.
83
83
The result kind is always the largest kind of the inputs.
84
- Note that NumPy considers ``float32 `` to have a lower precision than ``int32 ``
85
- and ``uint32 ``, but makes an exception for ``float64 `` (and higher).
84
+ Note that ``float32 `` has a lower precision than ``int32 `` or ``uint32 `` and
85
+ is thus sorted slightly to the left in the schematic. This is because
86
+ ``float32 `` cannot represent all ``int32 `` values exactly.
87
+ However, for practical reasons, NumPy allows promoting ``int64 `` to ``float64 ``
88
+ effectively considering them to have the same precision.
86
89
87
90
The Python scalars are inserted at the very left of each "kind" and the
88
- Python integer does not distinguish signed and unsigned.
91
+ Python integer does not distinguish signed and unsigned. NumPy promotion
92
+ thus uses the following, ordered, kind categories:
93
+
94
+ * `boolean `
95
+ * `integral `: signed or unsigned integers
96
+ * `inexact `: floating point numbers and complex floating point numbers
97
+
89
98
When promoting a Python scalar with a dtype of lower kind
90
- category (boolean, integral, inexact) with a higher one, we use the
99
+ category (` boolean < integral < inexact ` ) with a higher one, we use the
91
100
minimum/default precision: that is ``float64 ``, ``complex128 `` or ``int64 ``
92
101
(``int32 `` is used on some systems, e.g. windows).
93
102
@@ -207,7 +216,7 @@ Motivation and Scope
207
216
====================
208
217
209
218
The motivation for changing the behaviour with respect to inspecting the value
210
- of Python scalars and NumPy scalars/0-D arrays is two -fold:
219
+ of Python scalars and NumPy scalars/0-D arrays is three -fold:
211
220
212
221
1. The special handling of NumPy scalars/0-D arrays as well as the value
213
222
inspection can be very surprising to users,
@@ -217,6 +226,8 @@ of Python scalars and NumPy scalars/0-D arrays is two-fold:
217
226
Currently, this leads to a dual implementation of a new and an old (value
218
227
sensitive) system. Fixing this will greatly simplify the internal logic
219
228
and make results more consistent.
229
+ 3. It largely aligns with the choice of other projects like `JAX ` and
230
+ `data-apis.org ` (see also `Related Work `).
220
231
221
232
We believe that the proposal of "weak" Python scalars will help users by
222
233
providing a clear mental model for which datatype an operation will
@@ -285,8 +296,8 @@ Impact on operators and functions involving NumPy arrays or scalars
285
296
-------------------------------------------------------------------
286
297
287
298
The main impact on operations not involving Python scalars (``float ``, ``int ``,
288
- ``complex ``) will be that 0-D arrays and NumPy scalars will never behave
289
- value-sensitive .
299
+ ``complex ``) will be that operations on 0-D arrays and NumPy scalars will never
300
+ depend on their values .
290
301
This removes currently surprising cases. For example::
291
302
292
303
np.arange(10, dtype=np.uint8) + np.int64(1)
@@ -545,15 +556,19 @@ At no time is the value used to decide the result of this promotion. The value
545
556
considered when it is converted to the new dtype; this may raise an error.
546
557
547
558
548
-
549
-
550
559
Related Work
551
560
============
552
561
562
+ Different Python projects that fill a similar space to NumPy prefer the weakly
563
+ typed Python scalars as proposed in this NEP. Details of these may differ
564
+ or be unspecified though:
565
+
553
566
* `JAX promotion `_ also uses the weak-scalar concept. However, it makes use
554
567
of it also for most functions. JAX further stores the "weak-type" information
555
568
on the array: ``jnp.array(1) `` remains weakly typed.
556
569
570
+ * `data-apis.org `_ also suggests this weak-scalar logic for the Python scalars.
571
+
557
572
558
573
Implementation
559
574
==============
@@ -736,6 +751,8 @@ References and Footnotes
736
751
737
752
.. _JAX promotion : https://jax.readthedocs.io/en/latest/type_promotion.html
738
753
754
+ .. _data-apis.org : https://data-apis.org/array-api/latest/API_specification/type_promotion.html
755
+
739
756
.. [2 ] https://github.com/numpy/numpy/pull/21103/files#r814188019
740
757
741
758
Copyright
0 commit comments