Skip to content

Commit b441471

Browse files
committed
Copy 2022.12 spec with preserved git history
2 parents c280699 + 988f2f8 commit b441471

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+10032
-0
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
.. _array-object:
2+
3+
Array object
4+
============
5+
6+
Array API specification for array object attributes and methods.
7+
8+
A conforming implementation of the array API standard must provide and support an array object having the following attributes and methods.
9+
10+
Furthermore, a conforming implementation of the array API standard must support array objects of arbitrary rank ``N`` (i.e., number of dimensions), where ``N`` is greater than or equal to zero.
11+
12+
.. note::
13+
Conforming implementations must support zero-dimensional arrays.
14+
15+
Apart from array object attributes, such as ``ndim``, ``device``, and ``dtype``, all operations in this standard return arrays (or tuples of arrays), including those operations, such as ``mean``, ``var``, and ``std``, from which some common array libraries (e.g., NumPy) return scalar values.
16+
17+
*Rationale: always returning arrays is necessary to (1) support accelerator libraries where non-array return values could force device synchronization and (2) support delayed execution models where an array represents a future value.*
18+
19+
-------------------------------------------------
20+
21+
.. _operators:
22+
23+
Operators
24+
---------
25+
26+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators.
27+
28+
Arithmetic Operators
29+
~~~~~~~~~~~~~~~~~~~~
30+
31+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python arithmetic operators.
32+
33+
- ``+x``: :meth:`.array.__pos__`
34+
35+
- `operator.pos(x) <https://docs.python.org/3/library/operator.html#operator.pos>`_
36+
- `operator.__pos__(x) <https://docs.python.org/3/library/operator.html#operator.__pos__>`_
37+
38+
- `-x`: :meth:`.array.__neg__`
39+
40+
- `operator.neg(x) <https://docs.python.org/3/library/operator.html#operator.neg>`_
41+
- `operator.__neg__(x) <https://docs.python.org/3/library/operator.html#operator.__neg__>`_
42+
43+
- `x1 + x2`: :meth:`.array.__add__`
44+
45+
- `operator.add(x1, x2) <https://docs.python.org/3/library/operator.html#operator.add>`_
46+
- `operator.__add__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__add__>`_
47+
48+
- `x1 - x2`: :meth:`.array.__sub__`
49+
50+
- `operator.sub(x1, x2) <https://docs.python.org/3/library/operator.html#operator.sub>`_
51+
- `operator.__sub__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__sub__>`_
52+
53+
- `x1 * x2`: :meth:`.array.__mul__`
54+
55+
- `operator.mul(x1, x2) <https://docs.python.org/3/library/operator.html#operator.mul>`_
56+
- `operator.__mul__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__mul__>`_
57+
58+
- `x1 / x2`: :meth:`.array.__truediv__`
59+
60+
- `operator.truediv(x1,x2) <https://docs.python.org/3/library/operator.html#operator.truediv>`_
61+
- `operator.__truediv__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__truediv__>`_
62+
63+
- `x1 // x2`: :meth:`.array.__floordiv__`
64+
65+
- `operator.floordiv(x1, x2) <https://docs.python.org/3/library/operator.html#operator.floordiv>`_
66+
- `operator.__floordiv__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__floordiv__>`_
67+
68+
- `x1 % x2`: :meth:`.array.__mod__`
69+
70+
- `operator.mod(x1, x2) <https://docs.python.org/3/library/operator.html#operator.mod>`_
71+
- `operator.__mod__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__mod__>`_
72+
73+
- `x1 ** x2`: :meth:`.array.__pow__`
74+
75+
- `operator.pow(x1, x2) <https://docs.python.org/3/library/operator.html#operator.pow>`_
76+
- `operator.__pow__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__pow__>`_
77+
78+
Arithmetic operators should be defined for arrays having real-valued data types.
79+
80+
Array Operators
81+
~~~~~~~~~~~~~~~
82+
83+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python array operators.
84+
85+
- `x1 @ x2`: :meth:`.array.__matmul__`
86+
87+
- `operator.matmul(x1, x2) <https://docs.python.org/3/library/operator.html#operator.matmul>`_
88+
- `operator.__matmul__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__matmul__>`_
89+
90+
The matmul ``@`` operator should be defined for arrays having real-valued data types.
91+
92+
Bitwise Operators
93+
~~~~~~~~~~~~~~~~~
94+
95+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python bitwise operators.
96+
97+
- `~x`: :meth:`.array.__invert__`
98+
99+
- `operator.inv(x) <https://docs.python.org/3/library/operator.html#operator.inv>`_
100+
- `operator.invert(x) <https://docs.python.org/3/library/operator.html#operator.invert>`_
101+
- `operator.__inv__(x) <https://docs.python.org/3/library/operator.html#operator.__inv__>`_
102+
- `operator.__invert__(x) <https://docs.python.org/3/library/operator.html#operator.__invert__>`_
103+
104+
- `x1 & x2`: :meth:`.array.__and__`
105+
106+
- `operator.and(x1, x2) <https://docs.python.org/3/library/operator.html#operator.and>`_
107+
- `operator.__and__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__and__>`_
108+
109+
- `x1 | x2`: :meth:`.array.__or__`
110+
111+
- `operator.or(x1, x2) <https://docs.python.org/3/library/operator.html#operator.or>`_
112+
- `operator.__or__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__or__>`_
113+
114+
- `x1 ^ x2`: :meth:`.array.__xor__`
115+
116+
- `operator.xor(x1, x2) <https://docs.python.org/3/library/operator.html#operator.xor>`_
117+
- `operator.__xor__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__xor__>`_
118+
119+
- `x1 << x2`: :meth:`.array.__lshift__`
120+
121+
- `operator.lshift(x1, x2) <https://docs.python.org/3/library/operator.html#operator.lshift>`_
122+
- `operator.__lshift__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__lshift__>`_
123+
124+
- `x1 >> x2`: :meth:`.array.__rshift__`
125+
126+
- `operator.rshift(x1, x2) <https://docs.python.org/3/library/operator.html#operator.rshift>`_
127+
- `operator.__rshift__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__rshift__>`_
128+
129+
Bitwise operators should be defined for arrays having integer and boolean data types.
130+
131+
Comparison Operators
132+
~~~~~~~~~~~~~~~~~~~~
133+
134+
A conforming implementation of the array API standard must provide and support an array object supporting the following Python comparison operators.
135+
136+
- `x1 < x2`: :meth:`.array.__lt__`
137+
138+
- `operator.lt(x1, x2) <https://docs.python.org/3/library/operator.html#operator.lt>`_
139+
- `operator.__lt__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__lt__>`_
140+
141+
- `x1 <= x2`: :meth:`.array.__le__`
142+
143+
- `operator.le(x1, x2) <https://docs.python.org/3/library/operator.html#operator.le>`_
144+
- `operator.__le__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__le__>`_
145+
146+
- `x1 > x2`: :meth:`.array.__gt__`
147+
148+
- `operator.gt(x1, x2) <https://docs.python.org/3/library/operator.html#operator.gt>`_
149+
- `operator.__gt__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__gt__>`_
150+
151+
- `x1 >= x2`: :meth:`.array.__ge__`
152+
153+
- `operator.ge(x1, x2) <https://docs.python.org/3/library/operator.html#operator.ge>`_
154+
- `operator.__ge__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__ge__>`_
155+
156+
- `x1 == x2`: :meth:`.array.__eq__`
157+
158+
- `operator.eq(x1, x2) <https://docs.python.org/3/library/operator.html#operator.eq>`_
159+
- `operator.__eq__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__eq__>`_
160+
161+
- `x1 != x2`: :meth:`.array.__ne__`
162+
163+
- `operator.ne(x1, x2) <https://docs.python.org/3/library/operator.html#operator.ne>`_
164+
- `operator.__ne__(x1, x2) <https://docs.python.org/3/library/operator.html#operator.__ne__>`_
165+
166+
Comparison operators should be defined for arrays having any data type.
167+
168+
In-place Operators
169+
~~~~~~~~~~~~~~~~~~
170+
171+
A conforming implementation of the array API standard must provide and support an array object supporting the following in-place Python operators.
172+
173+
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`.
174+
175+
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``.
176+
177+
.. note::
178+
In-place operators must be supported as discussed in :ref:`copyview-mutability`.
179+
180+
Arithmetic Operators
181+
""""""""""""""""""""
182+
183+
- ``+=``. May be implemented via ``__iadd__``.
184+
- ``-=``. May be implemented via ``__isub__``.
185+
- ``*=``. May be implemented via ``__imul__``.
186+
- ``/=``. May be implemented via ``__itruediv__``.
187+
- ``//=``. May be implemented via ``__ifloordiv__``.
188+
- ``**=``. May be implemented via ``__ipow__``.
189+
- ``%=``. May be implemented via ``__imod__``.
190+
191+
Array Operators
192+
"""""""""""""""
193+
194+
- ``@=``. May be implemented via ``__imatmul__``.
195+
196+
Bitwise Operators
197+
"""""""""""""""""
198+
199+
- ``&=``. May be implemented via ``__iand__``.
200+
- ``|=``. May be implemented via ``__ior__``.
201+
- ``^=``. May be implemented via ``__ixor__``.
202+
- ``<<=``. May be implemented via ``__ilshift__``.
203+
- ``>>=``. May be implemented via ``__irshift__``.
204+
205+
Reflected Operators
206+
~~~~~~~~~~~~~~~~~~~
207+
208+
A conforming implementation of the array API standard must provide and support an array object supporting the following reflected operators.
209+
210+
The results of applying reflected operators must match their non-reflected equivalents.
211+
212+
.. note::
213+
All operators for which ``array <op> scalar`` is implemented must have an equivalent reflected operator implementation.
214+
215+
Arithmetic Operators
216+
""""""""""""""""""""
217+
218+
- ``__radd__``
219+
- ``__rsub__``
220+
- ``__rmul__``
221+
- ``__rtruediv__``
222+
- ``__rfloordiv__``
223+
- ``__rpow__``
224+
- ``__rmod__``
225+
226+
Array Operators
227+
"""""""""""""""
228+
229+
- ``__rmatmul__``
230+
231+
Bitwise Operators
232+
"""""""""""""""""
233+
234+
- ``__rand__``
235+
- ``__ror__``
236+
- ``__rxor__``
237+
- ``__rlshift__``
238+
- ``__rrshift__``
239+
240+
-------------------------------------------------
241+
242+
.. currentmodule:: array_api
243+
244+
Attributes
245+
----------
246+
..
247+
NOTE: please keep the attributes in alphabetical order
248+
249+
250+
.. autosummary::
251+
:toctree: generated
252+
:template: property.rst
253+
254+
array.dtype
255+
array.device
256+
array.mT
257+
array.ndim
258+
array.shape
259+
array.size
260+
array.T
261+
262+
-------------------------------------------------
263+
264+
Methods
265+
-------
266+
..
267+
NOTE: please keep the methods in alphabetical order
268+
269+
270+
.. autosummary::
271+
:toctree: generated
272+
:template: property.rst
273+
274+
array.__abs__
275+
array.__add__
276+
array.__and__
277+
array.__array_namespace__
278+
array.__bool__
279+
array.__complex__
280+
array.__dlpack__
281+
array.__dlpack_device__
282+
array.__eq__
283+
array.__float__
284+
array.__floordiv__
285+
array.__ge__
286+
array.__getitem__
287+
array.__gt__
288+
array.__index__
289+
array.__int__
290+
array.__invert__
291+
array.__le__
292+
array.__lshift__
293+
array.__lt__
294+
array.__matmul__
295+
array.__mod__
296+
array.__mul__
297+
array.__ne__
298+
array.__neg__
299+
array.__or__
300+
array.__pos__
301+
array.__pow__
302+
array.__rshift__
303+
array.__setitem__
304+
array.__sub__
305+
array.__truediv__
306+
array.__xor__
307+
array.to_device

0 commit comments

Comments
 (0)