|
| 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, at minimum, array objects of rank (i.e., number of dimensions) ``0``, ``1``, ``2``, ``3``, and ``4`` and must explicitly document their maximum supported rank ``N``. |
| 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 | +:meth:`.array.__lt__`, :meth:`.array.__le__`, :meth:`.array.__gt__`, :meth:`.array.__ge__` are only defined for arrays having real-valued data types. Other comparison operators should be defined for arrays having any data type. |
| 167 | +For backward compatibility, conforming implementations may support complex numbers; however, inequality comparison of complex numbers is unspecified and thus implementation-dependent (see :ref:`complex-number-ordering`). |
| 168 | + |
| 169 | +In-place Operators |
| 170 | +~~~~~~~~~~~~~~~~~~ |
| 171 | + |
| 172 | +A conforming implementation of the array API standard must provide and support an array object supporting the following in-place Python operators. |
| 173 | + |
| 174 | +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`. |
| 175 | + |
| 176 | +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``. |
| 177 | + |
| 178 | +.. note:: |
| 179 | + In-place operators must be supported as discussed in :ref:`copyview-mutability`. |
| 180 | + |
| 181 | +Arithmetic Operators |
| 182 | +"""""""""""""""""""" |
| 183 | + |
| 184 | +- ``+=``. May be implemented via ``__iadd__``. |
| 185 | +- ``-=``. May be implemented via ``__isub__``. |
| 186 | +- ``*=``. May be implemented via ``__imul__``. |
| 187 | +- ``/=``. May be implemented via ``__itruediv__``. |
| 188 | +- ``//=``. May be implemented via ``__ifloordiv__``. |
| 189 | +- ``**=``. May be implemented via ``__ipow__``. |
| 190 | +- ``%=``. May be implemented via ``__imod__``. |
| 191 | + |
| 192 | +Array Operators |
| 193 | +""""""""""""""" |
| 194 | + |
| 195 | +- ``@=``. May be implemented via ``__imatmul__``. |
| 196 | + |
| 197 | +Bitwise Operators |
| 198 | +""""""""""""""""" |
| 199 | + |
| 200 | +- ``&=``. May be implemented via ``__iand__``. |
| 201 | +- ``|=``. May be implemented via ``__ior__``. |
| 202 | +- ``^=``. May be implemented via ``__ixor__``. |
| 203 | +- ``<<=``. May be implemented via ``__ilshift__``. |
| 204 | +- ``>>=``. May be implemented via ``__irshift__``. |
| 205 | + |
| 206 | +Reflected Operators |
| 207 | +~~~~~~~~~~~~~~~~~~~ |
| 208 | + |
| 209 | +A conforming implementation of the array API standard must provide and support an array object supporting the following reflected operators. |
| 210 | + |
| 211 | +The results of applying reflected operators must match their non-reflected equivalents. |
| 212 | + |
| 213 | +.. note:: |
| 214 | + All operators for which ``array <op> scalar`` is implemented must have an equivalent reflected operator implementation. |
| 215 | + |
| 216 | +Arithmetic Operators |
| 217 | +"""""""""""""""""""" |
| 218 | + |
| 219 | +- ``__radd__`` |
| 220 | +- ``__rsub__`` |
| 221 | +- ``__rmul__`` |
| 222 | +- ``__rtruediv__`` |
| 223 | +- ``__rfloordiv__`` |
| 224 | +- ``__rpow__`` |
| 225 | +- ``__rmod__`` |
| 226 | + |
| 227 | +Array Operators |
| 228 | +""""""""""""""" |
| 229 | + |
| 230 | +- ``__rmatmul__`` |
| 231 | + |
| 232 | +Bitwise Operators |
| 233 | +""""""""""""""""" |
| 234 | + |
| 235 | +- ``__rand__`` |
| 236 | +- ``__ror__`` |
| 237 | +- ``__rxor__`` |
| 238 | +- ``__rlshift__`` |
| 239 | +- ``__rrshift__`` |
| 240 | + |
| 241 | +------------------------------------------------- |
| 242 | + |
| 243 | +.. currentmodule:: array_api |
| 244 | + |
| 245 | +Attributes |
| 246 | +---------- |
| 247 | +.. |
| 248 | + NOTE: please keep the attributes in alphabetical order |
| 249 | +
|
| 250 | + |
| 251 | +.. autosummary:: |
| 252 | + :toctree: generated |
| 253 | + :template: property.rst |
| 254 | + |
| 255 | + array.dtype |
| 256 | + array.device |
| 257 | + array.mT |
| 258 | + array.ndim |
| 259 | + array.shape |
| 260 | + array.size |
| 261 | + array.T |
| 262 | + |
| 263 | +------------------------------------------------- |
| 264 | + |
| 265 | +Methods |
| 266 | +------- |
| 267 | +.. |
| 268 | + NOTE: please keep the methods in alphabetical order |
| 269 | +
|
| 270 | + |
| 271 | +.. autosummary:: |
| 272 | + :toctree: generated |
| 273 | + :template: property.rst |
| 274 | + |
| 275 | + array.__abs__ |
| 276 | + array.__add__ |
| 277 | + array.__and__ |
| 278 | + array.__array_namespace__ |
| 279 | + array.__bool__ |
| 280 | + array.__complex__ |
| 281 | + array.__dlpack__ |
| 282 | + array.__dlpack_device__ |
| 283 | + array.__eq__ |
| 284 | + array.__float__ |
| 285 | + array.__floordiv__ |
| 286 | + array.__ge__ |
| 287 | + array.__getitem__ |
| 288 | + array.__gt__ |
| 289 | + array.__index__ |
| 290 | + array.__int__ |
| 291 | + array.__invert__ |
| 292 | + array.__le__ |
| 293 | + array.__lshift__ |
| 294 | + array.__lt__ |
| 295 | + array.__matmul__ |
| 296 | + array.__mod__ |
| 297 | + array.__mul__ |
| 298 | + array.__ne__ |
| 299 | + array.__neg__ |
| 300 | + array.__or__ |
| 301 | + array.__pos__ |
| 302 | + array.__pow__ |
| 303 | + array.__rshift__ |
| 304 | + array.__setitem__ |
| 305 | + array.__sub__ |
| 306 | + array.__truediv__ |
| 307 | + array.__xor__ |
| 308 | + array.to_device |
0 commit comments