Skip to content

Commit f72ba68

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

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
@@ -87,11 +87,37 @@ def __add__(self, other: Self | int | float, /) -> Self:
8787
...
8888

8989

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

0 commit comments

Comments
 (0)