Skip to content

Commit fe85984

Browse files
authored
fix: add right operators (#32)
1 parent 7855178 commit fe85984

File tree

5 files changed

+1477
-0
lines changed

5 files changed

+1477
-0
lines changed

src/array_api/_2022_12.py

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,380 @@ def to_device(self, device: TDevice, /, *, stream: int | Any | None = None) -> S
11541154
"""
11551155
...
11561156

1157+
def __radd__(self, other: int | float | complex | Self, /) -> Self:
1158+
"""
1159+
Calculates the sum for each element of an array instance with the respective element of the array ``other``.
1160+
1161+
Parameters
1162+
----------
1163+
self
1164+
array instance (augend array). Should have a numeric data type.
1165+
other: Union[int, float, complex, array]
1166+
addend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1167+
1168+
Returns
1169+
-------
1170+
out: array
1171+
an array containing the element-wise sums. The returned array must have a data type determined by :ref:`type-promotion`.
1172+
1173+
Notes
1174+
-----
1175+
1176+
.. note::
1177+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.add`.
1178+
1179+
.. versionchanged:: 2022.12
1180+
Added complex data type support.
1181+
1182+
"""
1183+
...
1184+
1185+
def __rand__(self, other: int | bool | Self, /) -> Self:
1186+
"""
1187+
Evaluates ``self_i & other_i`` for each element of an array instance with the respective element of the array ``other``.
1188+
1189+
Parameters
1190+
----------
1191+
self
1192+
array instance. Should have an integer or boolean data type.
1193+
other: Union[int, bool, array]
1194+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
1195+
1196+
Returns
1197+
-------
1198+
out: array
1199+
an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1200+
1201+
1202+
.. note::
1203+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_and`.
1204+
1205+
"""
1206+
...
1207+
1208+
def __rfloordiv__(self, other: int | float | Self, /) -> Self:
1209+
"""
1210+
Evaluates ``self_i // other_i`` for each element of an array instance with the respective element of the array ``other``.
1211+
1212+
.. note::
1213+
For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined.
1214+
1215+
Parameters
1216+
----------
1217+
self
1218+
array instance. Should have a real-valued data type.
1219+
other: Union[int, float, array]
1220+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
1221+
1222+
Returns
1223+
-------
1224+
out: array
1225+
an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1226+
1227+
1228+
.. note::
1229+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.floor_divide`.
1230+
1231+
"""
1232+
...
1233+
1234+
def __rlshift__(self, other: int | Self, /) -> Self:
1235+
"""
1236+
Evaluates ``self_i << other_i`` for each element of an array instance with the respective element of the array ``other``.
1237+
1238+
Parameters
1239+
----------
1240+
self
1241+
array instance. Should have an integer data type.
1242+
other: Union[int, array]
1243+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer data type. Each element must be greater than or equal to ``0``.
1244+
1245+
Returns
1246+
-------
1247+
out: array
1248+
an array containing the element-wise results. The returned array must have the same data type as ``self``.
1249+
1250+
1251+
.. note::
1252+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_left_shift`.
1253+
1254+
"""
1255+
...
1256+
1257+
def __rmatmul__(self, other: Self, /) -> Self:
1258+
"""
1259+
Computes the matrix product.
1260+
1261+
.. note::
1262+
The ``matmul`` function must implement the same semantics as the built-in ``@`` operator (see `PEP 465 <https://www.python.org/dev/peps/pep-0465>`_).
1263+
1264+
Parameters
1265+
----------
1266+
self
1267+
array instance. Should have a numeric data type. Must have at least one dimension. If ``self`` is one-dimensional having shape ``(M,)`` and ``other`` has more than one dimension, ``self`` must be promoted to a two-dimensional array by prepending ``1`` to its dimensions (i.e., must have shape ``(1, M)``). After matrix multiplication, the prepended dimensions in the returned array must be removed. If ``self`` has more than one dimension (including after vector-to-matrix promotion), ``shape(self)[:-2]`` must be compatible with ``shape(other)[:-2]`` (after vector-to-matrix promotion) (see :ref:`broadcasting`). If ``self`` has shape ``(..., M, K)``, the innermost two dimensions form matrices on which to perform matrix multiplication.
1268+
other: array
1269+
other array. Should have a numeric data type. Must have at least one dimension. If ``other`` is one-dimensional having shape ``(N,)`` and ``self`` has more than one dimension, ``other`` must be promoted to a two-dimensional array by appending ``1`` to its dimensions (i.e., must have shape ``(N, 1)``). After matrix multiplication, the appended dimensions in the returned array must be removed. If ``other`` has more than one dimension (including after vector-to-matrix promotion), ``shape(other)[:-2]`` must be compatible with ``shape(self)[:-2]`` (after vector-to-matrix promotion) (see :ref:`broadcasting`). If ``other`` has shape ``(..., K, N)``, the innermost two dimensions form matrices on which to perform matrix multiplication.
1270+
1271+
1272+
.. note::
1273+
If either ``x1`` or ``x2`` has a complex floating-point data type, neither argument must be complex-conjugated or transposed. If conjugation and/or transposition is desired, these operations should be explicitly performed prior to computing the matrix product.
1274+
1275+
Returns
1276+
-------
1277+
out: array
1278+
- if both ``self`` and ``other`` are one-dimensional arrays having shape ``(N,)``, a zero-dimensional array containing the inner product as its only element.
1279+
- if ``self`` is a two-dimensional array having shape ``(M, K)`` and ``other`` is a two-dimensional array having shape ``(K, N)``, a two-dimensional array containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ and having shape ``(M, N)``.
1280+
- if ``self`` is a one-dimensional array having shape ``(K,)`` and ``other`` is an array having shape ``(..., K, N)``, an array having shape ``(..., N)`` (i.e., prepended dimensions during vector-to-matrix promotion must be removed) and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_.
1281+
- if ``self`` is an array having shape ``(..., M, K)`` and ``other`` is a one-dimensional array having shape ``(K,)``, an array having shape ``(..., M)`` (i.e., appended dimensions during vector-to-matrix promotion must be removed) and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_.
1282+
- if ``self`` is a two-dimensional array having shape ``(M, K)`` and ``other`` is an array having shape ``(..., K, N)``, an array having shape ``(..., M, N)`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
1283+
- if ``self`` is an array having shape ``(..., M, K)`` and ``other`` is a two-dimensional array having shape ``(K, N)``, an array having shape ``(..., M, N)`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
1284+
- if either ``self`` or ``other`` has more than two dimensions, an array having a shape determined by :ref:`broadcasting` ``shape(self)[:-2]`` against ``shape(other)[:-2]`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
1285+
- The returned array must have a data type determined by :ref:`type-promotion`.
1286+
1287+
Notes
1288+
-----
1289+
1290+
.. note::
1291+
Results must equal the results returned by the equivalent function :func:`~array_api.matmul`.
1292+
1293+
**Raises**
1294+
1295+
- if either ``self`` or ``other`` is a zero-dimensional array.
1296+
- if ``self`` is a one-dimensional array having shape ``(K,)``, ``other`` is a one-dimensional array having shape ``(L,)``, and ``K != L``.
1297+
- if ``self`` is a one-dimensional array having shape ``(K,)``, ``other`` is an array having shape ``(..., L, N)``, and ``K != L``.
1298+
- if ``self`` is an array having shape ``(..., M, K)``, ``other`` is a one-dimensional array having shape ``(L,)``, and ``K != L``.
1299+
- if ``self`` is an array having shape ``(..., M, K)``, ``other`` is an array having shape ``(..., L, N)``, and ``K != L``.
1300+
1301+
.. versionchanged:: 2022.12
1302+
Added complex data type support.
1303+
1304+
"""
1305+
...
1306+
1307+
def __rmod__(self, other: int | float | Self, /) -> Self:
1308+
"""
1309+
Evaluates ``self_i % other_i`` for each element of an array instance with the respective element of the array ``other``.
1310+
1311+
.. note::
1312+
For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined.
1313+
1314+
Parameters
1315+
----------
1316+
self
1317+
array instance. Should have a real-valued data type.
1318+
other: Union[int, float, array]
1319+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
1320+
1321+
Returns
1322+
-------
1323+
out: array
1324+
an array containing the element-wise results. Each element-wise result must have the same sign as the respective element ``other_i``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`.
1325+
1326+
1327+
.. note::
1328+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.remainder`.
1329+
1330+
"""
1331+
...
1332+
1333+
def __rmul__(self, other: int | float | complex | Self, /) -> Self:
1334+
"""
1335+
Calculates the product for each element of an array instance with the respective element of the array ``other``.
1336+
1337+
.. note::
1338+
Floating-point multiplication is not always associative due to finite precision.
1339+
1340+
Parameters
1341+
----------
1342+
self
1343+
array instance. Should have a numeric data type.
1344+
other: Union[int, float, complex, array]
1345+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1346+
1347+
Returns
1348+
-------
1349+
out: array
1350+
an array containing the element-wise products. The returned array must have a data type determined by :ref:`type-promotion`.
1351+
1352+
Notes
1353+
-----
1354+
1355+
.. note::
1356+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.multiply`.
1357+
1358+
.. versionchanged:: 2022.12
1359+
Added complex data type support.
1360+
1361+
"""
1362+
...
1363+
1364+
def __ror__(self, other: int | bool | Self, /) -> Self:
1365+
"""
1366+
Evaluates ``self_i | other_i`` for each element of an array instance with the respective element of the array ``other``.
1367+
1368+
Parameters
1369+
----------
1370+
self
1371+
array instance. Should have an integer or boolean data type.
1372+
other: Union[int, bool, array]
1373+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
1374+
1375+
Returns
1376+
-------
1377+
out: array
1378+
an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1379+
1380+
Notes
1381+
-----
1382+
1383+
.. note::
1384+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_or`.
1385+
1386+
"""
1387+
...
1388+
1389+
def __rpow__(self, other: int | float | complex | Self, /) -> Self:
1390+
"""
1391+
Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power of ``other_i`` (the exponent), where ``other_i`` is the corresponding element of the array ``other``.
1392+
1393+
.. note::
1394+
If both ``self`` and ``other`` have integer data types, the result of ``__pow__`` when `other_i` is negative (i.e., less than zero) is unspecified and thus implementation-dependent.
1395+
1396+
If ``self`` has an integer data type and ``other`` has a floating-point data type, behavior is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
1397+
1398+
Parameters
1399+
----------
1400+
self
1401+
array instance whose elements correspond to the exponentiation base. Should have a numeric data type.
1402+
other: Union[int, float, complex, array]
1403+
other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1404+
1405+
Returns
1406+
-------
1407+
out: array
1408+
an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1409+
1410+
Notes
1411+
-----
1412+
1413+
.. note::
1414+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.pow`.
1415+
1416+
.. versionchanged:: 2022.12
1417+
Added complex data type support.
1418+
1419+
"""
1420+
...
1421+
1422+
def __rrshift__(self, other: int | Self, /) -> Self:
1423+
"""
1424+
Evaluates ``self_i >> other_i`` for each element of an array instance with the respective element of the array ``other``.
1425+
1426+
Parameters
1427+
----------
1428+
self
1429+
array instance. Should have an integer data type.
1430+
other: Union[int, array]
1431+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer data type. Each element must be greater than or equal to ``0``.
1432+
1433+
Returns
1434+
-------
1435+
out: array
1436+
an array containing the element-wise results. The returned array must have the same data type as ``self``.
1437+
1438+
1439+
.. note::
1440+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_right_shift`.
1441+
1442+
"""
1443+
...
1444+
1445+
def __rsub__(self, other: int | float | complex | Self, /) -> Self:
1446+
"""
1447+
Calculates the difference for each element of an array instance with the respective element of the array ``other``.
1448+
1449+
The result of ``self_i - other_i`` must be the same as ``self_i + (-other_i)`` and must be governed by the same floating-point rules as addition (see :meth:`array.__add__`).
1450+
1451+
Parameters
1452+
----------
1453+
self
1454+
array instance (minuend array). Should have a numeric data type.
1455+
other: Union[int, float, complex, array]
1456+
subtrahend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1457+
1458+
Returns
1459+
-------
1460+
out: array
1461+
an array containing the element-wise differences. The returned array must have a data type determined by :ref:`type-promotion`.
1462+
1463+
Notes
1464+
-----
1465+
1466+
.. note::
1467+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.subtract`.
1468+
1469+
.. versionchanged:: 2022.12
1470+
Added complex data type support.
1471+
1472+
"""
1473+
...
1474+
1475+
def __rtruediv__(self, other: int | float | complex | Self, /) -> Self:
1476+
"""
1477+
Evaluates ``self_i / other_i`` for each element of an array instance with the respective element of the array ``other``.
1478+
1479+
.. note::
1480+
If one or both of ``self`` and ``other`` have integer data types, the result is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
1481+
1482+
Specification-compliant libraries may choose to raise an error or return an array containing the element-wise results. If an array is returned, the array must have a real-valued floating-point data type.
1483+
1484+
Parameters
1485+
----------
1486+
self
1487+
array instance. Should have a numeric data type.
1488+
other: Union[int, float, complex, array]
1489+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1490+
1491+
Returns
1492+
-------
1493+
out: array
1494+
an array containing the element-wise results. The returned array should have a floating-point data type determined by :ref:`type-promotion`.
1495+
1496+
Notes
1497+
-----
1498+
1499+
.. note::
1500+
Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.divide`.
1501+
1502+
.. versionchanged:: 2022.12
1503+
Added complex data type support.
1504+
1505+
"""
1506+
...
1507+
1508+
def __rxor__(self, other: int | bool | Self, /) -> Self:
1509+
"""
1510+
Evaluates ``self_i ^ other_i`` for each element of an array instance with the respective element of the array ``other``.
1511+
1512+
Parameters
1513+
----------
1514+
self
1515+
array instance. Should have an integer or boolean data type.
1516+
other: Union[int, bool, array]
1517+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
1518+
1519+
Returns
1520+
-------
1521+
out: array
1522+
an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1523+
1524+
1525+
.. note::
1526+
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_xor`.
1527+
1528+
"""
1529+
...
1530+
11571531

11581532
@runtime_checkable
11591533
class astype[TArray: Array, TDtype](Protocol):

0 commit comments

Comments
 (0)