Skip to content

Commit 169ea5e

Browse files
committed
refactor: update guidance to generalize across data type kinds
1 parent 1b056ad commit 169ea5e

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

src/array_api_stubs/_draft/data_type_functions.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,34 @@ def astype(
2121
device: Optional[device] = None,
2222
) -> array:
2323
"""
24-
Copies an array to a specified data type irrespective of :ref:`type-promotion` rules, or to a *kind* of data type.
25-
26-
.. note::
27-
Casting floating-point ``NaN`` and ``infinity`` values to integral data types is not specified and is implementation-dependent.
28-
29-
.. note::
30-
Casting a complex floating-point array to a real-valued data type should not be permitted.
31-
32-
Historically, when casting a complex floating-point array to a real-valued data type, libraries such as NumPy have discarded imaginary components such that, for a complex floating-point array ``x``, ``astype(x)`` equals ``astype(real(x))``). This behavior is considered problematic as the choice to discard the imaginary component is arbitrary and introduces more than one way to achieve the same outcome (i.e., for a complex floating-point array ``x``, ``astype(x)`` and ``astype(real(x))`` versus only ``astype(imag(x))``). Instead, in order to avoid ambiguity and to promote clarity, this specification requires that array API consumers explicitly express which component should be cast to a specified real-valued data type.
33-
34-
.. note::
35-
When casting a boolean input array to a real-valued data type, a value of ``True`` must cast to a real-valued number equal to ``1``, and a value of ``False`` must cast to a real-valued number equal to ``0``.
36-
37-
When casting a boolean input array to a complex floating-point data type, a value of ``True`` must cast to a complex number equal to ``1 + 0j``, and a value of ``False`` must cast to a complex number equal to ``0 + 0j``.
38-
39-
.. note::
40-
When casting a real-valued input array to ``bool``, a value of ``0`` must cast to ``False``, and a non-zero value must cast to ``True``.
41-
42-
When casting a complex floating-point array to ``bool``, a value of ``0 + 0j`` must cast to ``False``, and all other values must cast to ``True``.
24+
Copies an array to a specified data type or data type kind irrespective of :ref:`type-promotion` rules.
4325
4426
Parameters
4527
----------
4628
x: array
4729
array to cast.
4830
dtype: Union[dtype, str]
49-
desired data type or kind of data type. Supported kinds are:
31+
desired data type or data type kind.
32+
33+
- If ``dtype`` is a data type, the function must return an array having the specified data type.
34+
- If ``dtype`` is a data type kind,
5035
51-
- ``'signed integer'``: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``).
52-
- ``'complex floating'``: complex floating-point data types (e.g., ``complex64``, ``complex128``).
36+
- If the data type of ``x`` belongs to the specified data type kind, the function must return an array having the same data type as ``x``.
37+
- If the data type of ``x`` does not belong to the specified data type kind, the function cast the input array to a data type of the specified data type kind according to type promotion rules (see :ref:`type-promotion`) and the casting rules documented below.
38+
39+
- When applying type promotion rules, the returned array must have the lowest-precision data type belonging to the specified data type kind to which the data type of ``x`` promotes (e.g., if ``x`` is ``float32`` and the data type kind is ``'complex floating'``, then the returned array must have the data type ``complex64``; if ``x`` is ``uint16`` and the data type kind is ``'signed integer'``, then the returned array must have the data type ``int32``).
40+
- When type promotion rules are not specified between the data type of ``x`` and the specified data type kind (e.g., ``int16`` and ``'real floating'``) and there exists one or more data types belonging to the specified data kind in which the elements in ``x`` can be represented exactly (e.g., ``int32`` and ``float64``), the function must return an array having the smallest data type (in terms of range of values) capable of precisely representing the elements of ``x`` (e.g., if ``x`` is ``int16`` and the data type kind is ``'complex floating'``, then the returned array must have the data type ``complex64``; if ``x`` is `bool`` and the data type kind is ``integral``, then the returned array must have the data type ``int8``).
41+
- When type promotion rules are not specified between the data type of ``x`` and the specified data type kind and there neither exists a data type belonging to the specified data type in which the elements of ``x`` can be represented exactly (e.g., ``uint64`` and ``'real floating'``) nor are there applicable casting rules documented below, behavior is unspecified and thus implementation-defined.
42+
43+
The following data type kinds must be supported:
44+
45+
- ``'bool'``: boolean data types (e.g., ``bool``).
46+
- ``'signed integer'``: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``).
47+
- ``'unsigned integer'``: unsigned integer data types (e.g., ``uint8``, ``uint16``, ``uint32``, ``uint64``).
48+
- ``'integral'``: integer data types. Shorthand for ``('signed integer', 'unsigned integer')``.
49+
- ``'real floating'``: real-valued floating-point data types (e.g., ``float32``, ``float64``).
50+
- ``'complex floating'``: complex floating-point data types (e.g., ``complex64``, ``complex128``).
51+
- ``'numeric'``: numeric data types. Shorthand for ``('integral', 'real floating', 'complex floating')``.
5352
5453
copy: bool
5554
specifies whether to copy an array when the specified ``dtype`` matches the data type of the input array ``x``. If ``True``, a newly allocated array must always be returned. If ``False`` and the specified ``dtype`` matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Default: ``True``.
@@ -59,19 +58,25 @@ def astype(
5958
Returns
6059
-------
6160
out: array
62-
For ``dtype`` a data type, an array having the specified data type.
63-
For ``dtype`` a kind of data type:
64-
65-
- If ``x.dtype`` is already of that kind, the data type must be maintained.
66-
- Otherwise, ``x`` should be cast to a data type of that kind, according to the type promotion rules (see :ref:`type-promotion`) and the above notes.
67-
- Kinds must be interpreted as the lowest-precision standard data type of that kind for the purposes of type promotion. For example, ``astype(x, 'complex floating')`` will return an array with the data type ``complex64`` when ``x.dtype`` is ``float32``, since ``complex64`` is the result of promoting ``float32`` with the lowest-precision standard complex data type, ``complex64``.
68-
- Where type promotion is unspecified and thus implementation-specific, the result is also unspecified. For example, ``astype(x, 'complex floating')``, where ``x`` has data type ``int32``.
69-
70-
The returned array must have the same shape as ``x``.
61+
an array having the specified data type or data type kind. The returned array must have the same shape as ``x``.
7162
7263
Notes
7364
-----
7465
66+
- Casting floating-point ``NaN`` and ``infinity`` values to integral data types is not specified and is implementation-dependent.
67+
68+
- Casting a complex floating-point array to a real-valued data type should not be permitted.
69+
70+
Historically, when casting a complex floating-point array to a real-valued data type, libraries such as NumPy have discarded imaginary components such that, for a complex floating-point array ``x``, ``astype(x)`` equals ``astype(real(x))``). This behavior is considered problematic as the choice to discard the imaginary component is arbitrary and introduces more than one way to achieve the same outcome (i.e., for a complex floating-point array ``x``, ``astype(x)`` and ``astype(real(x))`` versus only ``astype(imag(x))``). Instead, in order to avoid ambiguity and to promote clarity, this specification requires that array API consumers explicitly express which component should be cast to a specified real-valued data type.
71+
72+
- When casting a boolean input array to a real-valued data type, a value of ``True`` must cast to a real-valued number equal to ``1``, and a value of ``False`` must cast to a real-valued number equal to ``0``.
73+
74+
- When casting a boolean input array to a complex floating-point data type, a value of ``True`` must cast to a complex number equal to ``1 + 0j``, and a value of ``False`` must cast to a complex number equal to ``0 + 0j``.
75+
76+
- When casting a real-valued input array to ``bool``, a value of ``0`` must cast to ``False``, and a non-zero value must cast to ``True``.
77+
78+
- When casting a complex floating-point array to ``bool``, a value of ``0 + 0j`` must cast to ``False``, and all other values must cast to ``True``.
79+
7580
.. versionchanged:: 2022.12
7681
Added complex data type support.
7782

0 commit comments

Comments
 (0)