Skip to content

Commit 0683c70

Browse files

Some content is hidden

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

54 files changed

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

0 commit comments

Comments
 (0)