Skip to content

Commit 97278f5

Browse files
committed
✨: add HasArraySub protocol to support subtraction operator for array classes
1 parent 2287748 commit 97278f5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,30 @@ def __add__(self, other: Self | int | float, /) -> Self:
7676
...
7777

7878

79+
class HasArraySub(Protocol):
80+
"""Protocol for array classes that support the subtraction operator."""
81+
82+
def __sub__(self, other: Self | int | float, /) -> Self:
83+
"""Calculates the difference for each element of an array instance with the respective element of the array other.
84+
85+
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 array.__add__()).
86+
87+
Args:
88+
other: subtrahend array. Must be compatible with self (see Broadcasting). Should have a numeric data type.
89+
90+
Returns:
91+
an array containing the element-wise differences. The returned array must have a data type determined by Type Promotion Rules.
92+
93+
""" # noqa: E501
94+
...
95+
96+
7997
class Array(
8098
HasArrayNamespace[NS_co],
8199
HasArrayPos,
82100
HasArrayNeg,
83101
HasArrayAdd,
102+
HasArraySub,
84103
Protocol,
85104
):
86105
"""Array API specification for array object attributes and methods."""

0 commit comments

Comments
 (0)