Skip to content

Commit e9d6d23

Browse files
committed
✨: add CanArraySub Protocol
Support subtraction operator for array classes Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent ebd7bf2 commit e9d6d23

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,37 @@ def __add__(self, other: Self | int | float, /) -> Self:
8989
...
9090

9191

92+
class CanArraySub(Protocol):
93+
"""Protocol for array classes that support the subtraction operator."""
94+
95+
def __sub__(self, other: Self | int | float, /) -> Self:
96+
"""Calculates the difference for each element of an array instance with the respective element of the array other.
97+
98+
The result of `self_i - other_i` must be the same as `self_i +
99+
(-other_i)` and must be governed by the same floating-point rules as
100+
addition (see `CanArrayAdd`).
101+
102+
Args:
103+
other: subtrahend array. Must be compatible with self (see
104+
Broadcasting). Should have a numeric data type.
105+
106+
Returns:
107+
Self: an array containing the element-wise differences. The returned
108+
array must have a data type determined by Type Promotion Rules.
109+
110+
See Also:
111+
array_api_typing.Subtract
112+
113+
""" # noqa: E501
114+
...
115+
116+
92117
class Array(
93118
HasArrayNamespace[NS_co],
94119
CanArrayPos,
95120
CanArrayNeg,
96121
CanArrayAdd,
122+
CanArraySub,
97123
Protocol,
98124
):
99125
"""Array API specification for array object attributes and methods."""

0 commit comments

Comments
 (0)