Skip to content

Commit 2e17ca1

Browse files
authored
Group operators into separate classes (#308)
* Group operators * Fix placement
1 parent 2b97674 commit 2e17ca1

File tree

1 file changed

+102
-70
lines changed

1 file changed

+102
-70
lines changed

spec/API_specification/array_object.md

+102-70
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,11 @@ A conforming implementation of the array API standard must provide and support a
1919

2020
## Operators
2121

22-
A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators:
22+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators.
2323

24-
- `x1 < x2`: [`__lt__(x1, x2)`](#__lt__self-other-)
25-
26-
- [`operator.lt(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.lt)
27-
- [`operator.__lt__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__lt__)
24+
### Arithmetic Operators
2825

29-
- `x1 <= x2`: [`__le__(x1, x2)`](#__le__self-other-)
30-
31-
- [`operator.le(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.le)
32-
- [`operator.__le__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__le__)
33-
34-
- `x1 > x2`: [`__gt__(x1, x2)`](#__gt__self-other-)
35-
36-
- [`operator.gt(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.gt)
37-
- [`operator.__gt__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__gt__)
38-
39-
- `x1 >= x2`: [`__ge__(x1, x2)`](#__ge__self-other-)
40-
41-
- [`operator.ge(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.ge)
42-
- [`operator.__ge__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__ge__)
43-
44-
- `x1 == x2`: [`__eq__(x1, x2)`](#__eq__self-other-)
45-
46-
- [`operator.eq(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.eq)
47-
- [`operator.__eq__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__eq__)
48-
49-
- `x1 != x2`: [`__ne__(x1, x2)`](#__ne__self-other-)
50-
51-
- [`operator.ne(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.ne)
52-
- [`operator.__ne__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__ne__)
26+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python arithmetic operators.
5327

5428
- `+x`: [`__pos__(x)`](#__pos__self-)
5529

@@ -96,11 +70,23 @@ A conforming implementation of the array API standard must provide and support a
9670
- [`operator.pow(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.pow)
9771
- [`operator.__pow__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__pow__)
9872

73+
Arithmetic operators should be defined for arrays having numeric data types.
74+
75+
### Array Operators
76+
77+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python array operators.
78+
9979
- `x1 @ x2`: [`__matmul__(x1, x2)`](#__matmul__self-other-)
10080

10181
- [`operator.matmul(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.matmul)
10282
- [`operator.__matmul__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__matmul__)
10383

84+
The matmul `@` operator should be defined for arrays having numeric data types.
85+
86+
### Bitwise Operators
87+
88+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python bitwise operators.
89+
10490
- `~x`: [`__invert__(x)`](#__invert__self-)
10591

10692
- [`operator.inv(x)`](https://docs.python.org/3/library/operator.html#operator.inv)
@@ -133,64 +119,110 @@ A conforming implementation of the array API standard must provide and support a
133119
- [`operator.rshift(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.rshift)
134120
- [`operator.__rshift__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__rshift__)
135121

122+
Bitwise operators should be defined for arrays having integer and boolean data types.
123+
124+
### Comparison Operators
125+
126+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python comparison operators.
127+
128+
- `x1 < x2`: [`__lt__(x1, x2)`](#__lt__self-other-)
129+
130+
- [`operator.lt(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.lt)
131+
- [`operator.__lt__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__lt__)
132+
133+
- `x1 <= x2`: [`__le__(x1, x2)`](#__le__self-other-)
134+
135+
- [`operator.le(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.le)
136+
- [`operator.__le__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__le__)
137+
138+
- `x1 > x2`: [`__gt__(x1, x2)`](#__gt__self-other-)
139+
140+
- [`operator.gt(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.gt)
141+
- [`operator.__gt__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__gt__)
142+
143+
- `x1 >= x2`: [`__ge__(x1, x2)`](#__ge__self-other-)
144+
145+
- [`operator.ge(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.ge)
146+
- [`operator.__ge__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__ge__)
147+
148+
- `x1 == x2`: [`__eq__(x1, x2)`](#__eq__self-other-)
149+
150+
- [`operator.eq(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.eq)
151+
- [`operator.__eq__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__eq__)
152+
153+
- `x1 != x2`: [`__ne__(x1, x2)`](#__ne__self-other-)
154+
155+
- [`operator.ne(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.ne)
156+
- [`operator.__ne__(x1, x2)`](https://docs.python.org/3/library/operator.html#operator.__ne__)
157+
158+
Comparison operators should be defined for arrays having any data type.
136159

137160
### In-place Operators
138161

139-
A conforming implementation of the array API standard must provide and support
140-
an array object supporting the following in-place Python operators:
141-
142-
- `+=`. May be implemented via `__iadd__`.
143-
- `-=`. May be implemented via `__isub__`.
144-
- `*=`. May be implemented via `__imul__`.
145-
- `/=`. May be implemented via `__itruediv__`.
146-
- `//=`. May be implemented via `__ifloordiv__`.
147-
- `**=`. May be implemented via `__ipow__`.
148-
- `@=`. May be implemented via `__imatmul__`.
149-
- `%=`. May be implemented via `__imod__`.
150-
- `&=`. May be implemented via `__iand__`.
151-
- `|=`. May be implemented via `__ior__`.
152-
- `^=`. May be implemented via `__ixor__`.
153-
- `<<=`. May be implemented via `__ilshift__`.
154-
- `>>=`. May be implemented via `__irshift__`.
155-
156-
An in-place operation must not change the dtype or shape of the in-place array
157-
as a result of {ref}`type-promotion` or {ref}`broadcasting`.
158-
159-
An in-place operation must have the same behavior (including special cases) as
160-
its respective binary (i.e., two operand, non-assignment) operation. For example,
161-
after in-place addition `x1 += x2`, the modified array `x1` must always equal the
162-
result of the equivalent binary arithmetic operation `x1 = x1 + x2`.
162+
A conforming implementation of the array API standard must provide and support an array object supporting the following in-place Python operators.
163163

164-
```{note}
164+
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`.
165+
166+
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`.
165167

168+
```{note}
166169
In-place operators must be supported as discussed in {ref}`copyview-mutability`.
167170
```
168171

172+
#### Arithmetic Operators
173+
174+
- `+=`. May be implemented via `__iadd__`.
175+
- `-=`. May be implemented via `__isub__`.
176+
- `*=`. May be implemented via `__imul__`.
177+
- `/=`. May be implemented via `__itruediv__`.
178+
- `//=`. May be implemented via `__ifloordiv__`.
179+
- `**=`. May be implemented via `__ipow__`.
180+
- `%=`. May be implemented via `__imod__`.
181+
182+
#### Array Operators
183+
184+
- `@=`. May be implemented via `__imatmul__`.
185+
186+
#### Bitwise Operators
187+
188+
- `&=`. May be implemented via `__iand__`.
189+
- `|=`. May be implemented via `__ior__`.
190+
- `^=`. May be implemented via `__ixor__`.
191+
- `<<=`. May be implemented via `__ilshift__`.
192+
- `>>=`. May be implemented via `__irshift__`.
193+
169194
### Reflected Operators
170195

171-
A conforming implementation of the array API standard must provide and support
172-
an array object supporting the following reflected operators:
173-
174-
- `__radd__`
175-
- `__rsub__`
176-
- `__rmul__`
177-
- `__rtruediv__`
178-
- `__rfloordiv__`
179-
- `__rpow__`
180-
- `__rmatmul__`
181-
- `__rmod__`
182-
- `__rand__`
183-
- `__ror__`
184-
- `__rxor__`
185-
- `__rlshift__`
186-
- `__rrshift__`
196+
A conforming implementation of the array API standard must provide and support an array object supporting the following reflected operators.
187197

188198
The results of applying reflected operators must match their non-reflected equivalents.
189199

190200
```{note}
191201
All operators for which `array <op> scalar` is implemented must have an equivalent reflected operator implementation.
192202
```
193203

204+
#### Arithmetic Operators
205+
206+
- `__radd__`
207+
- `__rsub__`
208+
- `__rmul__`
209+
- `__rtruediv__`
210+
- `__rfloordiv__`
211+
- `__rpow__`
212+
- `__rmod__`
213+
214+
#### Array Operators
215+
216+
- `__rmatmul__`
217+
218+
#### Bitwise Operators
219+
220+
- `__rand__`
221+
- `__ror__`
222+
- `__rxor__`
223+
- `__rlshift__`
224+
- `__rrshift__`
225+
194226
* * *
195227

196228
## Attributes

0 commit comments

Comments
 (0)