Skip to content

Commit cfa91d3

Browse files
authored
docs: clarify the expected results for in-place operations for immutable array libraries
PR-URL: #895 Closes: #828 Reviewed-by: Sebastian Berg <[email protected]>
1 parent 407a8ae commit cfa91d3

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spec/2023.12/API_specification/array_object.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ A conforming implementation of the array API standard must provide and support a
8787
- `operator.matmul(x1, x2) <https://docs.python.org/3/library/operator.html#operator.matmul>`_
8888
- `operator.__matmul__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__matmul__>`_
8989

90-
The matmul ``@`` operator should be defined for arrays having real-valued data types.
90+
The matmul ``@`` operator should be defined for arrays having numeric data types.
9191

9292
Bitwise Operators
9393
~~~~~~~~~~~~~~~~~

spec/draft/API_specification/array_object.rst

+18-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ A conforming implementation of the array API standard must provide and support a
8787
- `operator.matmul(x1, x2) <https://docs.python.org/3/library/operator.html#operator.matmul>`_
8888
- `operator.__matmul__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__matmul__>`_
8989

90-
The matmul ``@`` operator should be defined for arrays having real-valued data types.
90+
The matmul ``@`` operator should be defined for arrays having numeric data types.
9191

9292
Bitwise Operators
9393
~~~~~~~~~~~~~~~~~
@@ -169,14 +169,28 @@ For backward compatibility, conforming implementations may support complex numbe
169169
In-place Operators
170170
~~~~~~~~~~~~~~~~~~
171171

172-
A conforming implementation of the array API standard must provide and support an array object supporting the following in-place Python operators.
172+
.. note::
173+
In-place operations must be supported as discussed in :ref:`copyview-mutability`.
174+
175+
A conforming implementation of the array API standard must provide and support an array object supporting the following "in-place" Python operators.
176+
177+
.. note::
178+
This specification refers to the following operators as "in-place" as that is what these operators are called in `Python <https://docs.python.org/3/library/operator.html#in-place-operators>`. However, conforming array libraries which do not support array mutation may choose to not explicitly implement in-place Python operators. When a library does not implement a method corresponding to an in-place Python operator, Python falls back to the equivalent method for the corresponding binary arithmetic operation.
173179

174180
An in-place operation must not change the data type or shape of the in-place array as a result of :ref:`type-promotion` or :ref:`broadcasting`.
175181

176-
An in-place operation must have the same behavior (including special cases) as its respective binary (i.e., two operand, non-assignment) operation. For example, after in-place addition ``x1 += x2``, the modified array ``x1`` must always equal the result of the equivalent binary arithmetic operation ``x1 = x1 + x2``.
182+
Let ``x1 += x2`` be a representative in-place operation. If, after applying type promotion (see :ref:`type-promotion`) to in-place operands ``x1`` and ``x2``, the resulting data type is equal to the data type of the array on the left-hand side of the operation (i.e., ``x1``), then an in-place operation must have the same behavior (including special cases) as the respective binary (i.e., two operand, non-assignment) operation. In this case, for the in-place addition ``x1 += x2``, the modified array ``x1`` must always equal the result of the equivalent binary arithmetic operation ``x1[...] = x1 + x2``.
183+
184+
If, however, after applying type promotion (see :ref:`type-promotion`) to in-place operands, the resulting data type is not equal to the data type of the array on the left-hand side of the operation, then a conforming implementation may return results which differ from the respective binary operation due to casting behavior and selection of the operation's intermediate precision. The choice of casting behavior and intermediate precision is unspecified and thus implementation-defined.
177185

178186
.. note::
179-
In-place operators must be supported as discussed in :ref:`copyview-mutability`.
187+
Let ``x1`` be the operand on the left-hand side and ``x2`` be the operand on the right-hand side of an in-place operation. Consumers of the array API standard are advised of the following considerations when using in-place operations:
188+
189+
1. In-place operations do not guarantee in-place mutation. A conforming library may or may not support in-place mutation.
190+
2. If, after applying broadcasting (see :ref:`broadcasting`) to in-place operands, the resulting shape is not equal to the shape of ``x1``, in-place operators may raise an exception.
191+
3. If, after applying type promotion (see :ref:`type-promotion`) to in-place operands, the resulting data type is not equal to the data type of ``x1``, the resulting data type may not equal the data type of ``x1`` and the operation's intermediate precision may be that of ``x1``, even if the promoted data type between ``x1`` and ``x2`` would have higher precision.
192+
193+
In general, for in-place operations, consumers of the array API standard are advised to ensure operands have the same data type and broadcast to the shape of the operand on the left-hand side of the operation in order to maximize portability.
180194

181195
Arithmetic Operators
182196
""""""""""""""""""""

0 commit comments

Comments
 (0)