Skip to content

Commit 1b4c218

Browse files
authored
Add complex number support to astype (#445)
* Add complex number support to `astype` * Update note * Disallow casting a complex floating-point data type to a real-valued data type
1 parent cb2d7bd commit 1b4c218

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

spec/API_specification/array_api/data_type_functions.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,28 @@ def astype(x: array, dtype: dtype, /, *, copy: bool = True) -> array:
88
Casting floating-point ``NaN`` and ``infinity`` values to integral data types is not specified and is implementation-dependent.
99
1010
.. note::
11-
When casting a boolean input array to a real-valued data type, a value of ``True`` must cast to a numeric value equal to ``1``, and a value of ``False`` must cast to a numeric value equal to ``0``.
11+
Casting a complex floating-point array to a real-valued data type should not be permitted.
1212
13+
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.
14+
15+
.. note::
16+
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``.
17+
18+
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``.
19+
20+
.. note::
1321
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``.
1422
23+
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+
1525
Parameters
1626
----------
1727
x: array
1828
array to cast.
1929
dtype: dtype
2030
desired data type.
2131
copy: bool
22-
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 must be returned. Default: ``True``.
32+
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``.
2333
2434
Returns
2535
-------

0 commit comments

Comments
 (0)