xbarray 0.0.1a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xbarray might be problematic. Click here for more details.

Files changed (35) hide show
  1. array_api_typing/__init__.py +9 -0
  2. array_api_typing/typing_2024_12/__init__.py +12 -0
  3. array_api_typing/typing_2024_12/_api_constant.py +32 -0
  4. array_api_typing/typing_2024_12/_api_fft_typing.py +717 -0
  5. array_api_typing/typing_2024_12/_api_linalg_typing.py +897 -0
  6. array_api_typing/typing_2024_12/_api_return_typing.py +103 -0
  7. array_api_typing/typing_2024_12/_api_typing.py +5855 -0
  8. array_api_typing/typing_2024_12/_array_typing.py +1265 -0
  9. array_api_typing/typing_compat/__init__.py +12 -0
  10. array_api_typing/typing_compat/_api_typing.py +27 -0
  11. array_api_typing/typing_compat/_array_typing.py +36 -0
  12. array_api_typing/typing_extra/__init__.py +12 -0
  13. array_api_typing/typing_extra/_api_typing.py +651 -0
  14. array_api_typing/typing_extra/_at.py +87 -0
  15. xbarray/__init__.py +1 -0
  16. xbarray/base/__init__.py +1 -0
  17. xbarray/base/base.py +199 -0
  18. xbarray/common/implementations.py +61 -0
  19. xbarray/jax/__init__.py +25 -0
  20. xbarray/jax/_extra.py +98 -0
  21. xbarray/jax/_typing.py +15 -0
  22. xbarray/jax/random.py +116 -0
  23. xbarray/numpy/__init__.py +19 -0
  24. xbarray/numpy/_extra.py +83 -0
  25. xbarray/numpy/_typing.py +14 -0
  26. xbarray/numpy/random.py +106 -0
  27. xbarray/pytorch/__init__.py +20 -0
  28. xbarray/pytorch/_extra.py +109 -0
  29. xbarray/pytorch/_typing.py +13 -0
  30. xbarray/pytorch/random.py +102 -0
  31. xbarray-0.0.1a1.dist-info/METADATA +14 -0
  32. xbarray-0.0.1a1.dist-info/RECORD +35 -0
  33. xbarray-0.0.1a1.dist-info/WHEEL +5 -0
  34. xbarray-0.0.1a1.dist-info/licenses/LICENSE +21 -0
  35. xbarray-0.0.1a1.dist-info/top_level.txt +2 -0
@@ -0,0 +1,1265 @@
1
+ from typing import Protocol, TypeVar, Optional, Any, Tuple, Union, Type, TypeAlias
2
+ from types import EllipsisType
3
+ from abc import abstractmethod
4
+ from enum import Enum
5
+
6
+ __all__ = [
7
+ 'SetIndex',
8
+ 'GetIndex',
9
+ 'Array',
10
+ 'PyCapsule',
11
+ 'SupportsDLPack',
12
+ 'Device',
13
+ 'DType',
14
+ ]
15
+
16
+ Device = TypeVar('Device')
17
+ DType = TypeVar('DType')
18
+
19
+ PyCapsule = object
20
+ class SupportsDLPack(Protocol):
21
+ @abstractmethod
22
+ def __dlpack__(
23
+ self,
24
+ /,
25
+ *,
26
+ stream: Optional[Union[int, Any]] = None,
27
+ max_version: Optional[tuple[int, int]] = None,
28
+ dl_device: Optional[tuple[Enum, int]] = None,
29
+ copy: Optional[bool] = None,
30
+ ) -> PyCapsule:
31
+ """
32
+ Exports the array for consumption by :func:`~array_api.from_dlpack` as a DLPack capsule.
33
+
34
+ Parameters
35
+ ----------
36
+ self: _ARR_C
37
+ array instance.
38
+ stream: Optional[Union[int, Any]]
39
+ for CUDA and ROCm, a Python integer representing a pointer to a stream, on devices that support streams. ``stream`` is provided by the consumer to the producer to instruct the producer to ensure that operations can safely be performed on the array (e.g., by inserting a dependency between streams via "wait for event"). The pointer must be an integer larger than or equal to ``-1`` (see below for allowed values on each platform). If ``stream`` is ``-1``, the value may be used by the consumer to signal "producer must not perform any synchronization". The ownership of the stream stays with the consumer. On CPU and other device types without streams, only ``None`` is accepted.
40
+
41
+ For other device types which do have a stream, queue, or similar synchronization/ordering mechanism, the most appropriate type to use for ``stream`` is not yet determined. E.g., for SYCL one may want to use an object containing an in-order ``cl::sycl::queue``. This is allowed when libraries agree on such a convention, and may be standardized in a future version of this API standard.
42
+
43
+ .. note::
44
+ Support for a ``stream`` value other than ``None`` is optional and implementation-dependent.
45
+
46
+ Device-specific values of ``stream`` for CUDA:
47
+
48
+ - ``None``: producer must assume the legacy default stream (default).
49
+ - ``1``: the legacy default stream.
50
+ - ``2``: the per-thread default stream.
51
+ - ``> 2``: stream number represented as a Python integer.
52
+ - ``0`` is disallowed due to its ambiguity: ``0`` could mean either ``None``, ``1``, or ``2``.
53
+
54
+ Device-specific values of ``stream`` for ROCm:
55
+
56
+ - ``None``: producer must assume the legacy default stream (default).
57
+ - ``0``: the default stream.
58
+ - ``> 2``: stream number represented as a Python integer.
59
+ - Using ``1`` and ``2`` is not supported.
60
+
61
+ .. note::
62
+ When ``dl_device`` is provided explicitly, ``stream`` must be a valid
63
+ construct for the specified device type. In particular, when ``kDLCPU``
64
+ is in use, ``stream`` must be ``None`` and a synchronization must be
65
+ performed to ensure data safety.
66
+
67
+ .. admonition:: Tip
68
+ :class: important
69
+
70
+ It is recommended that implementers explicitly handle streams. If
71
+ they use the legacy default stream, specifying ``1`` (CUDA) or ``0``
72
+ (ROCm) is preferred. ``None`` is a safe default for developers who do
73
+ not want to think about stream handling at all, potentially at the
74
+ cost of more synchronizations than necessary.
75
+ max_version: Optional[tuple[int, int]]
76
+ the maximum DLPack version that the *consumer* (i.e., the caller of
77
+ ``__dlpack__``) supports, in the form of a 2-tuple ``(major, minor)``.
78
+ This method may return a capsule of version ``max_version`` (recommended
79
+ if it does support that), or of a different version.
80
+ This means the consumer must verify the version even when
81
+ `max_version` is passed.
82
+ dl_device: Optional[tuple[enum.Enum, int]]
83
+ the DLPack device type. Default is ``None``, meaning the exported capsule
84
+ should be on the same device as ``self`` is. When specified, the format
85
+ must be a 2-tuple, following that of the return value of :meth:`array.__dlpack_device__`.
86
+ If the device type cannot be handled by the producer, this function must
87
+ raise ``BufferError``.
88
+
89
+ The v2023.12 standard only mandates that a compliant library should offer a way for
90
+ ``__dlpack__`` to return a capsule referencing an array whose underlying memory is
91
+ accessible to the Python interpreter (represented by the ``kDLCPU`` enumerator in DLPack).
92
+ If a copy must be made to enable this support but ``copy`` is set to ``False``, the
93
+ function must raise ``BufferError``.
94
+
95
+ Other device kinds will be considered for standardization in a future version of this
96
+ API standard.
97
+ copy: Optional[bool]
98
+ boolean indicating whether or not to copy the input. If ``True``, the
99
+ function must always copy (performed by the producer; see also :ref:`copy-keyword-argument`). If ``False``, the
100
+ function must never copy, and raise a ``BufferError`` in case a copy is
101
+ deemed necessary (e.g. if a cross-device data movement is requested, and
102
+ it is not possible without a copy). If ``None``, the function must reuse
103
+ the existing memory buffer if possible and copy otherwise. Default: ``None``.
104
+
105
+ When a copy happens, the ``DLPACK_FLAG_BITMASK_IS_COPIED`` flag must be set.
106
+
107
+ .. note::
108
+ If a copy happens, and if the consumer-provided ``stream`` and ``dl_device``
109
+ can be understood by the producer, the copy must be performed over ``stream``.
110
+
111
+ Returns
112
+ -------
113
+ capsule: PyCapsule
114
+ a DLPack capsule for the array. See :ref:`data-interchange` for details.
115
+
116
+ Raises
117
+ ------
118
+ BufferError
119
+ Implementations should raise ``BufferError`` when the data cannot
120
+ be exported as DLPack (e.g., incompatible dtype or strides). Other
121
+ errors are raised when export fails for other reasons (e.g., incorrect
122
+ arguments passed or out of memory).
123
+
124
+ Notes
125
+ -----
126
+ The DLPack version scheme is SemVer, where the major DLPack versions
127
+ represent ABI breaks, and minor versions represent ABI-compatible additions
128
+ (e.g., new enum values for new data types or device types).
129
+
130
+ The ``max_version`` keyword was introduced in v2023.12, and goes
131
+ together with the ``DLManagedTensorVersioned`` struct added in DLPack
132
+ 1.0. This keyword may not be used by consumers until a later time after
133
+ introduction, because producers may implement the support at a different
134
+ point in time.
135
+
136
+ It is recommended for the producer to use this logic in the implementation
137
+ of ``__dlpack__``:
138
+
139
+ .. code:: python
140
+
141
+ if max_version is None:
142
+ # Keep and use the DLPack 0.X implementation
143
+ # Note: from March 2025 onwards (but ideally as late as
144
+ # possible), it's okay to raise BufferError here
145
+ else:
146
+ # We get to produce `DLManagedTensorVersioned` now. Note that
147
+ # our_own_dlpack_version is the max version that the *producer*
148
+ # supports and fills in the `DLManagedTensorVersioned::version`
149
+ # field
150
+ if max_version >= our_own_dlpack_version:
151
+ # Consumer understands us, just return a Capsule with our max version
152
+ elif max_version[0] == our_own_dlpack_version[0]:
153
+ # major versions match, we should still be fine here -
154
+ # return our own max version
155
+ else:
156
+ # if we're at a higher major version internally, did we
157
+ # keep an implementation of the older major version around?
158
+ # For example, if the producer is on DLPack 1.x and the consumer
159
+ # is 0.y, can the producer still export a capsule containing
160
+ # DLManagedTensor and not DLManagedTensorVersioned?
161
+ # If so, use that. Else, the producer should raise a BufferError
162
+ # here to tell users that the consumer's max_version is too
163
+ # old to allow the data exchange to happen.
164
+
165
+ And this logic for the consumer in :func:`~array_api.from_dlpack`:
166
+
167
+ .. code:: python
168
+
169
+ try:
170
+ x.__dlpack__(max_version=(1, 0), ...)
171
+ # if it succeeds, store info from the capsule named "dltensor_versioned",
172
+ # and need to set the name to "used_dltensor_versioned" when we're done
173
+ except TypeError:
174
+ x.__dlpack__(...)
175
+
176
+ This logic is also applicable to handling of the new ``dl_device`` and ``copy``
177
+ keywords.
178
+
179
+ DLPack 1.0 added a flag to indicate that the array is read-only
180
+ (``DLPACK_FLAG_BITMASK_READ_ONLY``). A consumer that does not support
181
+ read-only arrays should ignore this flag (this is preferred over
182
+ raising an exception; the user is then responsible for ensuring the
183
+ memory isn't modified).
184
+
185
+ .. versionchanged:: 2022.12
186
+ Added BufferError.
187
+
188
+ .. versionchanged:: 2023.12
189
+ Added the ``max_version``, ``dl_device``, and ``copy`` keyword arguments.
190
+
191
+ .. versionchanged:: 2023.12
192
+ Added recommendation for handling read-only arrays.
193
+
194
+ .. versionchanged:: 2024.12
195
+ Resolved conflicting exception guidance.
196
+ """
197
+ pass
198
+
199
+ @abstractmethod
200
+ # def __dlpack_device__(self, /) -> Tuple[Enum, int]:
201
+ def __dlpack_device__(self) -> Any:
202
+ """
203
+ Returns device type and device ID in DLPack format. Meant for use within :func:`~array_api.from_dlpack`.
204
+
205
+ Parameters
206
+ ----------
207
+ self: _ARR_C
208
+ array instance.
209
+
210
+ Returns
211
+ -------
212
+ device: Tuple[Enum, int]
213
+ a tuple ``(device_type, device_id)`` in DLPack format. Valid device type enum members are:
214
+
215
+ ::
216
+
217
+ CPU = 1
218
+ CUDA = 2
219
+ CPU_PINNED = 3
220
+ OPENCL = 4
221
+ VULKAN = 7
222
+ METAL = 8
223
+ VPI = 9
224
+ ROCM = 10
225
+ CUDA_MANAGED = 13
226
+ ONE_API = 14
227
+ """
228
+ pass
229
+
230
+ """
231
+ From https://github.com/data-apis/array-api/blob/main/src/array_api_stubs/_2024_12/_types.py
232
+ """
233
+ SetIndex = Union[ # type: ignore[explicit-any]
234
+ int, slice, EllipsisType, "Array", Tuple[Union[int, slice, EllipsisType, "Array"], ...]
235
+ ]
236
+ GetIndex = Union[ # type: ignore[explicit-any]
237
+ SetIndex, None, Tuple[Union[int, slice, EllipsisType, None, "Array"], ...]
238
+ ]
239
+
240
+ _ARR_C = TypeVar("_ARR_C", bound='Array')
241
+ class Array(SupportsDLPack, Protocol):
242
+ def __init__(self: _ARR_C) -> None:
243
+ """Initialize the attributes for the array object class."""
244
+
245
+ @property
246
+ def dtype(self: _ARR_C) -> DType:
247
+ """
248
+ Data type of the array elements.
249
+
250
+ Returns
251
+ -------
252
+ out: dtype
253
+ array data type.
254
+ """
255
+
256
+ @property
257
+ def device(self: _ARR_C) -> Device:
258
+ """
259
+ Hardware device the array data resides on.
260
+
261
+ Returns
262
+ -------
263
+ out: device
264
+ a ``device`` object (see :ref:`device-support`).
265
+ """
266
+
267
+ @property
268
+ def mT(self: _ARR_C) -> _ARR_C:
269
+ """
270
+ Transpose of a matrix (or a stack of matrices).
271
+
272
+ If an array instance has fewer than two dimensions, an error should be raised.
273
+
274
+ Returns
275
+ -------
276
+ out: _ARR_C
277
+ array whose last two dimensions (axes) are permuted in reverse order relative to original array (i.e., for an array instance having shape ``(..., M, N)``, the returned array must have shape ``(..., N, M)``). The returned array must have the same data type as the original array.
278
+ """
279
+
280
+ @property
281
+ def ndim(self: _ARR_C) -> int:
282
+ """
283
+ Number of array dimensions (axes).
284
+
285
+ Returns
286
+ -------
287
+ out: int
288
+ number of array dimensions (axes).
289
+ """
290
+
291
+ @property
292
+ def shape(self: _ARR_C) -> Tuple[Optional[int], ...]:
293
+ """
294
+ Array dimensions.
295
+
296
+ Returns
297
+ -------
298
+ out: Tuple[Optional[int], ...]
299
+ array dimensions. An array dimension must be ``None`` if and only if a dimension is unknown.
300
+
301
+
302
+ .. note::
303
+ For array libraries having graph-based computational models, array dimensions may be unknown due to data-dependent operations (e.g., boolean indexing; ``A[:, B > 0]``) and thus cannot be statically resolved without knowing array contents.
304
+
305
+ .. note::
306
+ The returned value should be a tuple; however, where warranted, an array library may choose to return a custom shape object. If an array library returns a custom shape object, the object must be immutable, must support indexing for dimension retrieval, and must behave similarly to a tuple.
307
+ """
308
+
309
+ @property
310
+ def size(self: _ARR_C) -> Optional[int]:
311
+ """
312
+ Number of elements in an array.
313
+
314
+ .. note::
315
+ This must equal the product of the array's dimensions.
316
+
317
+ Returns
318
+ -------
319
+ out: Optional[int]
320
+ number of elements in an array. The returned value must be ``None`` if and only if one or more array dimensions are unknown.
321
+
322
+
323
+ .. note::
324
+ For array libraries having graph-based computational models, an array may have unknown dimensions due to data-dependent operations.
325
+ """
326
+
327
+ @property
328
+ def T(self: _ARR_C) -> _ARR_C:
329
+ """
330
+ Transpose of the array.
331
+
332
+ The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
333
+
334
+ Returns
335
+ -------
336
+ out: _ARR_C
337
+ two-dimensional array whose first and last dimensions (axes) are permuted in reverse order relative to original array. The returned array must have the same data type as the original array.
338
+
339
+
340
+ .. note::
341
+ Limiting the transpose to two-dimensional arrays (matrices) deviates from the NumPy et al practice of reversing all axes for arrays having more than two-dimensions. This is intentional, as reversing all axes was found to be problematic (e.g., conflicting with the mathematical definition of a transpose which is limited to matrices; not operating on batches of matrices; et cetera). In order to reverse all axes, one is recommended to use the functional ``permute_dims`` interface found in this specification.
342
+ """
343
+
344
+ def __abs__(self: _ARR_C, /) -> _ARR_C:
345
+ """
346
+ Calculates the absolute value for each element of an array instance.
347
+
348
+ For real-valued input arrays, the element-wise result has the same magnitude as the respective element in ``x`` but has positive sign.
349
+
350
+ .. note::
351
+ For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
352
+
353
+ Parameters
354
+ ----------
355
+ self: _ARR_C
356
+ array instance. Should have a numeric data type.
357
+
358
+ Returns
359
+ -------
360
+ out: _ARR_C
361
+ an array containing the element-wise absolute value. If ``self`` has a real-valued data type, the returned array must have the same data type as ``self``. If ``self`` has a complex floating-point data type, the returned arrayed must have a real-valued floating-point data type whose precision matches the precision of ``self`` (e.g., if ``self`` is ``complex128``, then the returned array must have a ``float64`` data type).
362
+
363
+ Notes
364
+ -----
365
+
366
+ .. note::
367
+ Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.abs`.
368
+
369
+ .. versionchanged:: 2022.12
370
+ Added complex data type support.
371
+ """
372
+
373
+ def __add__(self: _ARR_C, other: Union[int, float, complex, _ARR_C], /) -> _ARR_C:
374
+ """
375
+ Calculates the sum for each element of an array instance with the respective element of the array ``other``.
376
+
377
+ Parameters
378
+ ----------
379
+ self: _ARR_C
380
+ array instance (augend array). Should have a numeric data type.
381
+ other: Union[int, float, array]
382
+ addend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
383
+
384
+ Returns
385
+ -------
386
+ out: _ARR_C
387
+ an array containing the element-wise sums. The returned array must have a data type determined by :ref:`type-promotion`.
388
+
389
+ Notes
390
+ -----
391
+
392
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.add`.
393
+
394
+ .. versionchanged:: 2022.12
395
+ Added complex data type support.
396
+ """
397
+
398
+ def __and__(self: _ARR_C, other: Union[int, bool, _ARR_C], /) -> _ARR_C:
399
+ """
400
+ Evaluates ``self_i & other_i`` for each element of an array instance with the respective element of the array ``other``.
401
+
402
+ Parameters
403
+ ----------
404
+ self: _ARR_C
405
+ array instance. Should have an integer or boolean data type.
406
+ other: Union[int, bool, array]
407
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
408
+
409
+ Returns
410
+ -------
411
+ out: _ARR_C
412
+ an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
413
+
414
+ Notes
415
+ -----
416
+
417
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_and`.
418
+ """
419
+
420
+ def __array_namespace__(
421
+ self: _ARR_C, /, *, api_version: Optional[str] = None
422
+ ) -> Any:
423
+ """
424
+ Returns an object that has all the array API functions on it.
425
+
426
+ Parameters
427
+ ----------
428
+ self: _ARR_C
429
+ array instance.
430
+ api_version: Optional[str]
431
+ string representing the version of the array API specification to be returned, in ``'YYYY.MM'`` form, for example, ``'2020.10'``. If it is ``None``, it should return the namespace corresponding to latest version of the array API specification. If the given version is invalid or not implemented for the given module, an error should be raised. Default: ``None``.
432
+
433
+ Returns
434
+ -------
435
+ out: Any
436
+ an object representing the array API namespace. It should have every top-level function defined in the specification as an attribute. It may contain other public names as well, but it is recommended to only include those names that are part of the specification.
437
+ """
438
+
439
+ def __bool__(self: _ARR_C, /) -> bool:
440
+ """
441
+ Converts a zero-dimensional array to a Python ``bool`` object.
442
+
443
+ Parameters
444
+ ----------
445
+ self: _ARR_C
446
+ zero-dimensional array instance.
447
+
448
+ Returns
449
+ -------
450
+ out: bool
451
+ a Python ``bool`` object representing the single element of the array.
452
+
453
+ Notes
454
+ -----
455
+
456
+ **Special cases**
457
+
458
+ For real-valued floating-point operands,
459
+
460
+ - If ``self`` is ``NaN``, the result is ``True``.
461
+ - If ``self`` is either ``+infinity`` or ``-infinity``, the result is ``True``.
462
+ - If ``self`` is either ``+0`` or ``-0``, the result is ``False``.
463
+
464
+ For complex floating-point operands, special cases must be handled as if the operation is implemented as the logical OR of ``bool(real(self))`` and ``bool(imag(self))``.
465
+
466
+ **Lazy implementations**
467
+
468
+ The Python language requires the return value to be of type ``bool``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead.
469
+
470
+ .. versionchanged:: 2022.12
471
+ Added boolean and complex data type support.
472
+
473
+ .. versionchanged:: 2023.12
474
+ Allowed lazy implementations to error.
475
+ """
476
+
477
+ def __complex__(self: _ARR_C, /) -> complex:
478
+ """
479
+ Converts a zero-dimensional array to a Python ``complex`` object.
480
+
481
+ Parameters
482
+ ----------
483
+ self: _ARR_C
484
+ zero-dimensional array instance.
485
+
486
+ Returns
487
+ -------
488
+ out: complex
489
+ a Python ``complex`` object representing the single element of the array instance.
490
+
491
+ Notes
492
+ -----
493
+
494
+ **Special cases**
495
+
496
+ For boolean operands,
497
+
498
+ - If ``self`` is ``True``, the result is ``1+0j``.
499
+ - If ``self`` is ``False``, the result is ``0+0j``.
500
+
501
+ For real-valued floating-point operands,
502
+
503
+ - If ``self`` is ``NaN``, the result is ``NaN + NaN j``.
504
+ - If ``self`` is ``+infinity``, the result is ``+infinity + 0j``.
505
+ - If ``self`` is ``-infinity``, the result is ``-infinity + 0j``.
506
+ - If ``self`` is a finite number, the result is ``self + 0j``.
507
+
508
+ **Lazy implementations**
509
+
510
+ The Python language requires the return value to be of type ``complex``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead.
511
+
512
+ .. versionadded:: 2022.12
513
+
514
+ .. versionchanged:: 2023.12
515
+ Allowed lazy implementations to error.
516
+ """
517
+
518
+ def __eq__(self: _ARR_C, other: Union[int, float, complex, bool, _ARR_C], /) -> _ARR_C:
519
+ r"""
520
+ Computes the truth value of ``self_i == other_i`` for each element of an array instance with the respective element of the array ``other``.
521
+
522
+ Parameters
523
+ ----------
524
+ self: _ARR_C
525
+ array instance. May have any data type.
526
+ other: Union[int, float, complex, bool, array]
527
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). May have any data type.
528
+
529
+ Returns
530
+ -------
531
+ out: _ARR_C
532
+ an array containing the element-wise results. The returned array must have a data type of ``bool``.
533
+
534
+ Notes
535
+ -----
536
+
537
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.equal`.
538
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
539
+
540
+ .. versionchanged:: 2022.12
541
+ Added complex data type support.
542
+
543
+ .. versionchanged:: 2024.12
544
+ Cross-kind comparisons are explicitly left unspecified.
545
+ """
546
+
547
+ def __float__(self: _ARR_C, /) -> float:
548
+ """
549
+ Converts a zero-dimensional array to a Python ``float`` object.
550
+
551
+ .. note::
552
+ Casting integer values outside the representable bounds of Python's float type is not specified and is implementation-dependent.
553
+
554
+ Parameters
555
+ ----------
556
+ self: _ARR_C
557
+ zero-dimensional array instance. Should have a real-valued or boolean data type. If ``self`` has a complex floating-point data type, the function must raise a ``TypeError``.
558
+
559
+ Returns
560
+ -------
561
+ out: float
562
+ a Python ``float`` object representing the single element of the array instance.
563
+
564
+ Notes
565
+ -----
566
+
567
+ **Special cases**
568
+
569
+ For boolean operands,
570
+
571
+ - If ``self`` is ``True``, the result is ``1``.
572
+ - If ``self`` is ``False``, the result is ``0``.
573
+
574
+ **Lazy implementations**
575
+
576
+ The Python language requires the return value to be of type ``float``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead.
577
+
578
+ .. versionchanged:: 2022.12
579
+ Added boolean and complex data type support.
580
+
581
+ .. versionchanged:: 2023.12
582
+ Allowed lazy implementations to error.
583
+ """
584
+
585
+ def __floordiv__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
586
+ """
587
+ Evaluates ``self_i // other_i`` for each element of an array instance with the respective element of the array ``other``.
588
+
589
+ .. note::
590
+ For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined.
591
+
592
+ Parameters
593
+ ----------
594
+ self: _ARR_C
595
+ array instance. Should have a real-valued data type.
596
+ other: Union[int, float, array]
597
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
598
+
599
+ Returns
600
+ -------
601
+ out: _ARR_C
602
+ an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
603
+
604
+
605
+ .. note::
606
+ Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.floor_divide`.
607
+ """
608
+
609
+ def __ge__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
610
+ """
611
+ Computes the truth value of ``self_i >= other_i`` for each element of an array instance with the respective element of the array ``other``.
612
+
613
+ Parameters
614
+ ----------
615
+ self: _ARR_C
616
+ array instance. Should have a real-valued data type.
617
+ other: Union[int, float, array]
618
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
619
+
620
+ Returns
621
+ -------
622
+ out: _ARR_C
623
+ an array containing the element-wise results. The returned array must have a data type of ``bool``.
624
+
625
+ Notes
626
+ -----
627
+
628
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.greater_equal`.
629
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
630
+ - 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`).
631
+
632
+ .. versionchanged:: 2024.12
633
+ Cross-kind comparisons are explicitly left unspecified.
634
+ """
635
+
636
+ def __getitem__(
637
+ self: _ARR_C,
638
+ key: Union[
639
+ int,
640
+ slice,
641
+ EllipsisType,
642
+ None,
643
+ Tuple[Union[int, slice, EllipsisType, _ARR_C, None], ...],
644
+ _ARR_C,
645
+ ],
646
+ /,
647
+ ) -> _ARR_C:
648
+ """
649
+ Returns ``self[key]``.
650
+
651
+ Parameters
652
+ ----------
653
+ self: _ARR_C
654
+ array instance.
655
+ key: Union[int, slice, Ellipsis, None, Tuple[Union[int, slice, Ellipsis, array, None], ...], array]
656
+ index key.
657
+
658
+ Returns
659
+ -------
660
+ out: _ARR_C
661
+ an array containing the accessed value(s). The returned array must have the same data type as ``self``.
662
+
663
+ Notes
664
+ -----
665
+
666
+ - See :ref:`indexing` for details on supported indexing semantics.
667
+ - When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
668
+
669
+ .. versionchanged:: 2024.12
670
+ Clarified that iteration is defined for one-dimensional arrays.
671
+ """
672
+
673
+ def __gt__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
674
+ """
675
+ Computes the truth value of ``self_i > other_i`` for each element of an array instance with the respective element of the array ``other``.
676
+
677
+ Parameters
678
+ ----------
679
+ self: _ARR_C
680
+ array instance. Should have a real-valued data type.
681
+ other: Union[int, float, array]
682
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
683
+
684
+ Returns
685
+ -------
686
+ out: _ARR_C
687
+ an array containing the element-wise results. The returned array must have a data type of ``bool``.
688
+
689
+ Notes
690
+ -----
691
+
692
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.greater`.
693
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
694
+ - 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`).
695
+
696
+ .. versionchanged:: 2024.12
697
+ Cross-kind comparisons are explicitly left unspecified.
698
+ """
699
+
700
+ def __index__(self: _ARR_C, /) -> int:
701
+ """
702
+ Converts a zero-dimensional integer array to a Python ``int`` object.
703
+
704
+ .. note::
705
+ This method is called to implement `operator.index() <https://docs.python.org/3/reference/datamodel.html#object.__index__>`_. See also `PEP 357 <https://www.python.org/dev/peps/pep-0357/>`_.
706
+
707
+ Parameters
708
+ ----------
709
+ self: _ARR_C
710
+ zero-dimensional array instance. Should have an integer data type. If ``self`` has a floating-point data type, the function must raise a ``TypeError``.
711
+
712
+ Returns
713
+ -------
714
+ out: int
715
+ a Python ``int`` object representing the single element of the array instance.
716
+
717
+ Notes
718
+ -----
719
+
720
+ **Lazy implementations**
721
+
722
+ The Python language requires the return value to be of type ``int``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead.
723
+
724
+ .. versionchanged:: 2023.12
725
+ Allowed lazy implementations to error.
726
+ """
727
+
728
+ def __int__(self: _ARR_C, /) -> int:
729
+ """
730
+ Converts a zero-dimensional array to a Python ``int`` object.
731
+
732
+ Parameters
733
+ ----------
734
+ self: _ARR_C
735
+ zero-dimensional array instance. Should have a real-valued or boolean data type. If ``self`` has a complex floating-point data type, the function must raise a ``TypeError``.
736
+
737
+ Returns
738
+ -------
739
+ out: int
740
+ a Python ``int`` object representing the single element of the array instance.
741
+
742
+ Notes
743
+ -----
744
+
745
+ **Special cases**
746
+
747
+ For boolean operands,
748
+
749
+ - If ``self`` is ``True``, the result is ``1``.
750
+ - If ``self`` is ``False``, the result is ``0``.
751
+
752
+ For floating-point operands,
753
+
754
+ - If ``self`` is a finite number, the result is the integer part of ``self``.
755
+ - If ``self`` is ``-0``, the result is ``0``.
756
+
757
+ **Raises**
758
+
759
+ For floating-point operands,
760
+
761
+ - If ``self`` is either ``+infinity`` or ``-infinity``, raise ``OverflowError``.
762
+ - If ``self`` is ``NaN``, raise ``ValueError``.
763
+
764
+ Notes
765
+ -----
766
+
767
+ **Lazy implementations**
768
+
769
+ The Python language requires the return value to be of type ``int``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead.
770
+
771
+ .. versionchanged:: 2022.12
772
+ Added boolean and complex data type support.
773
+
774
+ .. versionchanged:: 2023.12
775
+ Allowed lazy implementations to error.
776
+ """
777
+
778
+ def __invert__(self: _ARR_C, /) -> _ARR_C:
779
+ """
780
+ Evaluates ``~self_i`` for each element of an array instance.
781
+
782
+ Parameters
783
+ ----------
784
+ self: _ARR_C
785
+ array instance. Should have an integer or boolean data type.
786
+
787
+ Returns
788
+ -------
789
+ out: _ARR_C
790
+ an array containing the element-wise results. The returned array must have the same data type as `self`.
791
+
792
+
793
+ .. note::
794
+ Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_invert`.
795
+ """
796
+
797
+ def __le__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
798
+ """
799
+ Computes the truth value of ``self_i <= other_i`` for each element of an array instance with the respective element of the array ``other``.
800
+
801
+ Parameters
802
+ ----------
803
+ self: _ARR_C
804
+ array instance. Should have a real-valued data type.
805
+ other: Union[int, float, array]
806
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
807
+
808
+ Returns
809
+ -------
810
+ out: _ARR_C
811
+ an array containing the element-wise results. The returned array must have a data type of ``bool``.
812
+
813
+ Notes
814
+ -----
815
+
816
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.less_equal`.
817
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
818
+ - 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`).
819
+
820
+ .. versionchanged:: 2024.12
821
+ Cross-kind comparisons are explicitly left unspecified.
822
+ """
823
+
824
+ def __lshift__(self: _ARR_C, other: Union[int, _ARR_C], /) -> _ARR_C:
825
+ """
826
+ Evaluates ``self_i << other_i`` for each element of an array instance with the respective element of the array ``other``.
827
+
828
+ Parameters
829
+ ----------
830
+ self: _ARR_C
831
+ array instance. Should have an integer data type.
832
+ other: Union[int, array]
833
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer data type. Each element must be greater than or equal to ``0``.
834
+
835
+ Returns
836
+ -------
837
+ out: _ARR_C
838
+ an array containing the element-wise results. The returned array must have the same data type as ``self``.
839
+
840
+ Notes
841
+ -----
842
+
843
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_left_shift`.
844
+ """
845
+
846
+ def __lt__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
847
+ """
848
+ Computes the truth value of ``self_i < other_i`` for each element of an array instance with the respective element of the array ``other``.
849
+
850
+ Parameters
851
+ ----------
852
+ self: _ARR_C
853
+ array instance. Should have a real-valued data type.
854
+ other: Union[int, float, array]
855
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
856
+
857
+ Returns
858
+ -------
859
+ out: _ARR_C
860
+ an array containing the element-wise results. The returned array must have a data type of ``bool``.
861
+
862
+ Notes
863
+ -----
864
+
865
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.less`.
866
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
867
+ - 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`).
868
+
869
+ .. versionchanged:: 2024.12
870
+ Cross-kind comparisons are explicitly left unspecified.
871
+ """
872
+
873
+ def __matmul__(self: _ARR_C, other: _ARR_C, /) -> _ARR_C:
874
+ """
875
+ Computes the matrix product.
876
+
877
+ .. note::
878
+ The ``matmul`` function must implement the same semantics as the built-in ``@`` operator (see `PEP 465 <https://www.python.org/dev/peps/pep-0465>`_).
879
+
880
+ Parameters
881
+ ----------
882
+ self: _ARR_C
883
+ array instance. Should have a numeric data type. Must have at least one dimension. If ``self`` is one-dimensional having shape ``(M,)`` and ``other`` has more than one dimension, ``self`` must be promoted to a two-dimensional array by prepending ``1`` to its dimensions (i.e., must have shape ``(1, M)``). After matrix multiplication, the prepended dimensions in the returned array must be removed. If ``self`` has more than one dimension (including after vector-to-matrix promotion), ``shape(self)[:-2]`` must be compatible with ``shape(other)[:-2]`` (after vector-to-matrix promotion) (see :ref:`broadcasting`). If ``self`` has shape ``(..., M, K)``, the innermost two dimensions form matrices on which to perform matrix multiplication.
884
+ other: _ARR_C
885
+ other array. Should have a numeric data type. Must have at least one dimension. If ``other`` is one-dimensional having shape ``(N,)`` and ``self`` has more than one dimension, ``other`` must be promoted to a two-dimensional array by appending ``1`` to its dimensions (i.e., must have shape ``(N, 1)``). After matrix multiplication, the appended dimensions in the returned array must be removed. If ``other`` has more than one dimension (including after vector-to-matrix promotion), ``shape(other)[:-2]`` must be compatible with ``shape(self)[:-2]`` (after vector-to-matrix promotion) (see :ref:`broadcasting`). If ``other`` has shape ``(..., K, N)``, the innermost two dimensions form matrices on which to perform matrix multiplication.
886
+
887
+
888
+ .. note::
889
+ If either ``x1`` or ``x2`` has a complex floating-point data type, neither argument must be complex-conjugated or transposed. If conjugation and/or transposition is desired, these operations should be explicitly performed prior to computing the matrix product.
890
+
891
+ Returns
892
+ -------
893
+ out: _ARR_C
894
+ - if both ``self`` and ``other`` are one-dimensional arrays having shape ``(N,)``, a zero-dimensional array containing the inner product as its only element.
895
+ - if ``self`` is a two-dimensional array having shape ``(M, K)`` and ``other`` is a two-dimensional array having shape ``(K, N)``, a two-dimensional array containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ and having shape ``(M, N)``.
896
+ - if ``self`` is a one-dimensional array having shape ``(K,)`` and ``other`` is an array having shape ``(..., K, N)``, an array having shape ``(..., N)`` (i.e., prepended dimensions during vector-to-matrix promotion must be removed) and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_.
897
+ - if ``self`` is an array having shape ``(..., M, K)`` and ``other`` is a one-dimensional array having shape ``(K,)``, an array having shape ``(..., M)`` (i.e., appended dimensions during vector-to-matrix promotion must be removed) and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_.
898
+ - if ``self`` is a two-dimensional array having shape ``(M, K)`` and ``other`` is an array having shape ``(..., K, N)``, an array having shape ``(..., M, N)`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
899
+ - if ``self`` is an array having shape ``(..., M, K)`` and ``other`` is a two-dimensional array having shape ``(K, N)``, an array having shape ``(..., M, N)`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
900
+ - if either ``self`` or ``other`` has more than two dimensions, an array having a shape determined by :ref:`broadcasting` ``shape(self)[:-2]`` against ``shape(other)[:-2]`` and containing the `conventional matrix product <https://en.wikipedia.org/wiki/Matrix_multiplication>`_ for each stacked matrix.
901
+ - The returned array must have a data type determined by :ref:`type-promotion`.
902
+
903
+ Notes
904
+ -----
905
+
906
+ .. note::
907
+ Results must equal the results returned by the equivalent function :func:`~array_api.matmul`.
908
+
909
+ **Raises**
910
+
911
+ - if either ``self`` or ``other`` is a zero-dimensional array.
912
+ - if ``self`` is a one-dimensional array having shape ``(K,)``, ``other`` is a one-dimensional array having shape ``(L,)``, and ``K != L``.
913
+ - if ``self`` is a one-dimensional array having shape ``(K,)``, ``other`` is an array having shape ``(..., L, N)``, and ``K != L``.
914
+ - if ``self`` is an array having shape ``(..., M, K)``, ``other`` is a one-dimensional array having shape ``(L,)``, and ``K != L``.
915
+ - if ``self`` is an array having shape ``(..., M, K)``, ``other`` is an array having shape ``(..., L, N)``, and ``K != L``.
916
+
917
+ .. versionchanged:: 2022.12
918
+ Added complex data type support.
919
+ """
920
+
921
+ def __mod__(self: _ARR_C, other: Union[int, float, _ARR_C], /) -> _ARR_C:
922
+ """
923
+ Evaluates ``self_i % other_i`` for each element of an array instance with the respective element of the array ``other``.
924
+
925
+ Parameters
926
+ ----------
927
+ self: _ARR_C
928
+ array instance. Should have a real-valued data type.
929
+ other: Union[int, float, array]
930
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
931
+
932
+ Returns
933
+ -------
934
+ out: _ARR_C
935
+ an array containing the element-wise results. Each element-wise result must have the same sign as the respective element ``other_i``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`.
936
+
937
+ Notes
938
+ -----
939
+
940
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.remainder`.
941
+ - For input arrays which promote to an integer data type, the result of division by zero is unspecified and thus implementation-defined.
942
+ """
943
+
944
+ def __mul__(self: _ARR_C, other: Union[int, float, complex, _ARR_C], /) -> _ARR_C:
945
+ r"""
946
+ Calculates the product for each element of an array instance with the respective element of the array ``other``.
947
+
948
+ .. note::
949
+ Floating-point multiplication is not always associative due to finite precision.
950
+
951
+ Parameters
952
+ ----------
953
+ self: _ARR_C
954
+ array instance. Should have a numeric data type.
955
+ other: Union[int, float, complex, array]
956
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
957
+
958
+ Returns
959
+ -------
960
+ out: _ARR_C
961
+ an array containing the element-wise products. The returned array must have a data type determined by :ref:`type-promotion`.
962
+
963
+ Notes
964
+ -----
965
+
966
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.multiply`.
967
+
968
+ .. versionchanged:: 2022.12
969
+ Added complex data type support.
970
+ """
971
+
972
+ def __ne__(self: _ARR_C, other: Union[int, float, complex, bool, _ARR_C], /) -> _ARR_C:
973
+ """
974
+ Computes the truth value of ``self_i != other_i`` for each element of an array instance with the respective element of the array ``other``.
975
+
976
+ Parameters
977
+ ----------
978
+ self: _ARR_C
979
+ array instance. May have any data type.
980
+ other: Union[int, float, complex, bool, array]
981
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). May have any data type.
982
+
983
+ Returns
984
+ -------
985
+ out: _ARR_C
986
+ an array containing the element-wise results. The returned array must have a data type of ``bool`` (i.e., must be a boolean array).
987
+
988
+ Notes
989
+ -----
990
+
991
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.not_equal`.
992
+ - Comparison of arrays without a corresponding promotable data type (see :ref:`type-promotion`) is undefined and thus implementation-dependent.
993
+
994
+ .. versionchanged:: 2022.12
995
+ Added complex data type support.
996
+
997
+ .. versionchanged:: 2024.12
998
+ Cross-kind comparisons are explicitly left unspecified.
999
+ """
1000
+
1001
+ def __neg__(self: _ARR_C, /) -> _ARR_C:
1002
+ """
1003
+ Evaluates ``-self_i`` for each element of an array instance.
1004
+
1005
+ .. note::
1006
+ For signed integer data types, the numerical negative of the minimum representable integer is implementation-dependent.
1007
+
1008
+ .. note::
1009
+ If ``self`` has a complex floating-point data type, both the real and imaginary components for each ``self_i`` must be negated (a result which follows from the rules of complex number multiplication).
1010
+
1011
+ Parameters
1012
+ ----------
1013
+ self: _ARR_C
1014
+ array instance. Should have a numeric data type.
1015
+
1016
+ Returns
1017
+ -------
1018
+ out: _ARR_C
1019
+ an array containing the evaluated result for each element in ``self``. The returned array must have a data type determined by :ref:`type-promotion`.
1020
+
1021
+ Notes
1022
+ -----
1023
+
1024
+ .. note::
1025
+ Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.negative`.
1026
+
1027
+ .. versionchanged:: 2022.12
1028
+ Added complex data type support.
1029
+ """
1030
+
1031
+ def __or__(self: _ARR_C, other: Union[int, bool, _ARR_C], /) -> _ARR_C:
1032
+ """
1033
+ Evaluates ``self_i | other_i`` for each element of an array instance with the respective element of the array ``other``.
1034
+
1035
+ Parameters
1036
+ ----------
1037
+ self: _ARR_C
1038
+ array instance. Should have an integer or boolean data type.
1039
+ other: Union[int, bool, array]
1040
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
1041
+
1042
+ Returns
1043
+ -------
1044
+ out: _ARR_C
1045
+ an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1046
+
1047
+ Notes
1048
+ -----
1049
+
1050
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_or`.
1051
+ """
1052
+
1053
+ def __pos__(self: _ARR_C, /) -> _ARR_C:
1054
+ """
1055
+ Evaluates ``+self_i`` for each element of an array instance.
1056
+
1057
+ Parameters
1058
+ ----------
1059
+ self: _ARR_C
1060
+ array instance. Should have a numeric data type.
1061
+
1062
+ Returns
1063
+ -------
1064
+ out: _ARR_C
1065
+ an array containing the evaluated result for each element. The returned array must have the same data type as ``self``.
1066
+
1067
+ Notes
1068
+ -----
1069
+
1070
+ .. note::
1071
+ Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.positive`.
1072
+
1073
+ .. versionchanged:: 2022.12
1074
+ Added complex data type support.
1075
+ """
1076
+
1077
+ def __pow__(self: _ARR_C, other: Union[int, float, complex, _ARR_C], /) -> _ARR_C:
1078
+ r"""
1079
+ Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power of ``other_i`` (the exponent), where ``other_i`` is the corresponding element of the array ``other``.
1080
+
1081
+ Parameters
1082
+ ----------
1083
+ self: _ARR_C
1084
+ array instance whose elements correspond to the exponentiation base. Should have a numeric data type.
1085
+ other: Union[int, float, complex, array]
1086
+ other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1087
+
1088
+ Returns
1089
+ -------
1090
+ out: _ARR_C
1091
+ an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1092
+
1093
+ Notes
1094
+ -----
1095
+
1096
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.pow`.
1097
+ - If both ``self`` and ``other`` have integer data types, the result of ``__pow__`` when `other_i` is negative (i.e., less than zero) is unspecified and thus implementation-dependent.
1098
+ - If ``self`` has an integer data type and ``other`` has a floating-point data type, behavior is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
1099
+
1100
+ .. versionchanged:: 2022.12
1101
+ Added complex data type support.
1102
+ """
1103
+
1104
+ def __rshift__(self: _ARR_C, other: Union[int, _ARR_C], /) -> _ARR_C:
1105
+ """
1106
+ Evaluates ``self_i >> other_i`` for each element of an array instance with the respective element of the array ``other``.
1107
+
1108
+ Parameters
1109
+ ----------
1110
+ self: _ARR_C
1111
+ array instance. Should have an integer data type.
1112
+ other: Union[int, array]
1113
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer data type. Each element must be greater than or equal to ``0``.
1114
+
1115
+ Returns
1116
+ -------
1117
+ out: _ARR_C
1118
+ an array containing the element-wise results. The returned array must have the same data type as ``self``.
1119
+
1120
+ Notes
1121
+ -----
1122
+
1123
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_right_shift`.
1124
+ """
1125
+
1126
+ def __setitem__(
1127
+ self: _ARR_C,
1128
+ key: Union[
1129
+ int, slice, EllipsisType, Tuple[Union[int, slice, EllipsisType, _ARR_C], ...], _ARR_C
1130
+ ],
1131
+ value: Union[int, float, complex, bool, _ARR_C],
1132
+ /,
1133
+ ) -> None:
1134
+ """
1135
+ Sets ``self[key]`` to ``value``.
1136
+
1137
+ Parameters
1138
+ ----------
1139
+ self: _ARR_C
1140
+ array instance.
1141
+ key: Union[int, slice, EllipsisType, Tuple[Union[int, slice, EllipsisType, _ARR_C], ...], array]
1142
+ index key.
1143
+ value: Union[int, float, complex, bool, array]
1144
+ value(s) to set. Must be compatible with ``self[key]`` (see :ref:`broadcasting`).
1145
+
1146
+ Notes
1147
+ -----
1148
+
1149
+ - See :ref:`indexing` for details on supported indexing semantics.
1150
+
1151
+ .. note::
1152
+ Indexing semantics when ``key`` is an integer array or a tuple of integers and integer arrays is currently unspecified and thus implementation-defined. This will be revisited in a future revision of this standard.
1153
+
1154
+ - Setting array values must not affect the data type of ``self``.
1155
+ - When ``value`` is a Python scalar (i.e., ``int``, ``float``, ``complex``, ``bool``), behavior must follow specification guidance on mixing arrays with Python scalars (see :ref:`type-promotion`).
1156
+ - When ``value`` is an ``array`` of a different data type than ``self``, how values are cast to the data type of ``self`` is implementation defined.
1157
+ """
1158
+
1159
+ def __sub__(self: _ARR_C, other: Union[int, float, complex, _ARR_C], /) -> _ARR_C:
1160
+ """
1161
+ Calculates the difference for each element of an array instance with the respective element of the array ``other``.
1162
+
1163
+ Parameters
1164
+ ----------
1165
+ self: _ARR_C
1166
+ array instance (minuend array). Should have a numeric data type.
1167
+ other: Union[int, float, complex, array]
1168
+ subtrahend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1169
+
1170
+ Returns
1171
+ -------
1172
+ out: _ARR_C
1173
+ an array containing the element-wise differences. The returned array must have a data type determined by :ref:`type-promotion`.
1174
+
1175
+ Notes
1176
+ -----
1177
+
1178
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.subtract`.
1179
+ - 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 :meth:`array.__add__`).
1180
+
1181
+ .. versionchanged:: 2022.12
1182
+ Added complex data type support.
1183
+ """
1184
+
1185
+ def __truediv__(self: _ARR_C, other: Union[int, float, complex, _ARR_C], /) -> _ARR_C:
1186
+ r"""
1187
+ Evaluates ``self_i / other_i`` for each element of an array instance with the respective element of the array ``other``.
1188
+
1189
+ Parameters
1190
+ ----------
1191
+ self: _ARR_C
1192
+ array instance. Should have a numeric data type.
1193
+ other: Union[int, float, complex, array]
1194
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
1195
+
1196
+ Returns
1197
+ -------
1198
+ out: _ARR_C
1199
+ an array containing the element-wise results. The returned array should have a floating-point data type determined by :ref:`type-promotion`.
1200
+
1201
+ Notes
1202
+ -----
1203
+
1204
+ - Element-wise results, including special cases, must equal the results returned by the equivalent element-wise function :func:`~array_api.divide`.
1205
+
1206
+ - If one or both of ``self`` and ``other`` have integer data types, the result is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
1207
+
1208
+ Specification-compliant libraries may choose to raise an error or return an array containing the element-wise results. If an array is returned, the array must have a real-valued floating-point data type.
1209
+
1210
+ .. versionchanged:: 2022.12
1211
+ Added complex data type support.
1212
+ """
1213
+
1214
+ def __xor__(self: _ARR_C, other: Union[int, bool, _ARR_C], /) -> _ARR_C:
1215
+ """
1216
+ Evaluates ``self_i ^ other_i`` for each element of an array instance with the respective element of the array ``other``.
1217
+
1218
+ Parameters
1219
+ ----------
1220
+ self: _ARR_C
1221
+ array instance. Should have an integer or boolean data type.
1222
+ other: Union[int, bool, array]
1223
+ other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have an integer or boolean data type.
1224
+
1225
+ Returns
1226
+ -------
1227
+ out: _ARR_C
1228
+ an array containing the element-wise results. The returned array must have a data type determined by :ref:`type-promotion`.
1229
+
1230
+ Notes
1231
+ -----
1232
+
1233
+ - Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.bitwise_xor`.
1234
+ """
1235
+
1236
+ def to_device(
1237
+ self: _ARR_C, device: Device, /, *, stream: Optional[Union[int, Any]] = None
1238
+ ) -> _ARR_C:
1239
+ """
1240
+ Copy the array from the device on which it currently resides to the specified ``device``.
1241
+
1242
+ Parameters
1243
+ ----------
1244
+ self: _ARR_C
1245
+ array instance.
1246
+ device: device
1247
+ a ``device`` object (see :ref:`device-support`).
1248
+ stream: Optional[Union[int, Any]]
1249
+ stream object to use during copy. In addition to the types supported in :meth:`array.__dlpack__`, implementations may choose to support any library-specific stream object with the caveat that any code using such an object would not be portable.
1250
+
1251
+ Returns
1252
+ -------
1253
+ out: _ARR_C
1254
+ an array with the same data and data type as ``self`` and located on the specified ``device``.
1255
+
1256
+
1257
+ Notes
1258
+ -----
1259
+
1260
+ - When a provided ``device`` object corresponds to the same device on which an array instance resides, implementations may choose to perform an explicit copy or return ``self``.
1261
+ - If ``stream`` is provided, the copy operation should be enqueued on the provided ``stream``; otherwise, the copy operation should be enqueued on the default stream/queue. Whether the copy is performed synchronously or asynchronously is implementation-dependent. Accordingly, if synchronization is required to guarantee data safety, this must be clearly explained in a conforming array library's documentation.
1262
+
1263
+ .. versionchanged:: 2023.12
1264
+ Clarified behavior when a provided ``device`` object corresponds to the device on which an array instance resides.
1265
+ """