(array-object)=
Array API specification for array object attributes and methods.
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.
- Positional parameters must be positional-only 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.
- Optional parameters must be keyword-only arguments.
- Broadcasting semantics must follow the semantics defined in {ref}
broadcasting
. - Unless stated otherwise, methods must support the data types defined in {ref}
data-types
. - Unless stated otherwise, methods must adhere to the type promotion rules defined in {ref}
type-promotion
. - Unless stated otherwise, floating-point operations must adhere to IEEE 754-2019.
(operators)=
A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators.
A conforming implementation of the array API standard must provide and support an array object supporting the following Python arithmetic operators.
-
+x
:__pos__(x)
-
-x
:__neg__(x)
-
x1 + x2
:__add__(x1, x2)
-
x1 - x2
:__sub__(x1, x2)
-
x1 * x2
:__mul__(x1, x2)
-
x1 / x2
:__truediv__(x1, x2)
-
x1 // x2
:__floordiv__(x1, x2)
-
x1 % x2
:__mod__(x1, x2)
-
x1 ** x2
:__pow__(x1, x2)
Arithmetic operators should be defined for arrays having numeric data types.
A conforming implementation of the array API standard must provide and support an array object supporting the following Python array operators.
The matmul @
operator should be defined for arrays having numeric data types.
A conforming implementation of the array API standard must provide and support an array object supporting the following Python bitwise operators.
-
~x
:__invert__(x)
-
x1 & x2
:__and__(x1, x2)
-
x1 | x2
:__or__(x1, x2)
-
x1 ^ x2
:__xor__(x1, x2)
-
x1 << x2
:__lshift__(x1, x2)
-
x1 >> x2
:__rshift__(x1, x2)
Bitwise operators should be defined for arrays having integer and boolean data types.
A conforming implementation of the array API standard must provide and support an array object supporting the following Python comparison operators.
-
x1 < x2
:__lt__(x1, x2)
-
x1 <= x2
:__le__(x1, x2)
-
x1 > x2
:__gt__(x1, x2)
-
x1 >= x2
:__ge__(x1, x2)
-
x1 == x2
:__eq__(x1, x2)
-
x1 != x2
:__ne__(x1, x2)
Comparison operators should be defined for arrays having any data type.
A conforming implementation of the array API standard must provide and support an array object supporting the following in-place Python operators.
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
.
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
.
In-place operators must be supported as discussed in {ref}`copyview-mutability`.
+=
. May be implemented via__iadd__
.-=
. May be implemented via__isub__
.*=
. May be implemented via__imul__
./=
. May be implemented via__itruediv__
.//=
. May be implemented via__ifloordiv__
.**=
. May be implemented via__ipow__
.%=
. May be implemented via__imod__
.
@=
. May be implemented via__imatmul__
.
&=
. May be implemented via__iand__
.|=
. May be implemented via__ior__
.^=
. May be implemented via__ixor__
.<<=
. May be implemented via__ilshift__
.>>=
. May be implemented via__irshift__
.
A conforming implementation of the array API standard must provide and support an array object supporting the following reflected operators.
The results of applying reflected operators must match their non-reflected equivalents.
All operators for which `array <op> scalar` is implemented must have an equivalent reflected operator implementation.
__radd__
__rsub__
__rmul__
__rtruediv__
__rfloordiv__
__rpow__
__rmod__
__rmatmul__
__rand__
__ror__
__rxor__
__rlshift__
__rrshift__
(attribute-dtype)=
Data type of the array elements.
-
out: <dtype>
- array data type.
(attribute-device)=
Hardware device the array data resides on.
-
out: <device>
- a
device
object (see {ref}device-support
).
- a
(attribute-mT)=
Transpose of a matrix (or a stack of matrices).
If an array instance has fewer than two dimensions, an error should be raised.
-
out: <array>
- 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.
- array whose last two dimensions (axes) are permuted in reverse order relative to original array (i.e., for an array instance having shape
(attribute-ndim)=
Number of array dimensions (axes).
-
out: int
- number of array dimensions (axes).
(attribute-shape)=
Array dimensions.
-
out: Tuple[ Optional[ int ], ... ]
- array dimensions. An array dimension must be
None
if and only if a dimension is unknown.
- array dimensions. An array dimension must be
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.
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.
(attribute-size)=
Number of elements in an array.
This must equal the product of the array's dimensions.
-
out: Optional[ int ]
- number of elements in an array. The returned value must be
None
if and only if one or more array dimensions are unknown.
- number of elements in an array. The returned value must be
For array libraries having graph-based computational models, an array may have unknown dimensions due to data-dependent operations.
(attribute-T)=
Transpose of the array.
The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
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.
-
out: <array>
- 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.
(method-abs)=
Calculates the absolute value for each element of an array instance (i.e., the element-wise result has the same magnitude as the respective element but has positive sign).
For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
For floating-point operands, let self
equal x
.
- If
x_i
isNaN
, the result isNaN
. - If
x_i
is-0
, the result is+0
. - If
x_i
is-infinity
, the result is+infinity
.
-
self: <array>
- array instance. Should have a numeric data type.
-
out: <array>
- an array containing the element-wise absolute value. The returned array must have the same data type as
self
.
- an array containing the element-wise absolute value. The returned array must have the same data type as
Element-wise results must equal the results returned by the equivalent element-wise function [`abs(x)`](elementwise_functions.md#absx-).
(method-add)=
Calculates the sum for each element of an array instance with the respective element of the array other
.
For floating-point operands, let self
equal x1
and other
equal x2
.
- If either
x1_i
orx2_i
isNaN
, the result isNaN
. - If
x1_i
is+infinity
andx2_i
is-infinity
, the result isNaN
. - If
x1_i
is-infinity
andx2_i
is+infinity
, the result isNaN
. - If
x1_i
is+infinity
andx2_i
is+infinity
, the result is+infinity
. - If
x1_i
is-infinity
andx2_i
is-infinity
, the result is-infinity
. - If
x1_i
is+infinity
andx2_i
is a finite number, the result is+infinity
. - If
x1_i
is-infinity
andx2_i
is a finite number, the result is-infinity
. - If
x1_i
is a finite number andx2_i
is+infinity
, the result is+infinity
. - If
x1_i
is a finite number andx2_i
is-infinity
, the result is-infinity
. - If
x1_i
is-0
andx2_i
is-0
, the result is-0
. - If
x1_i
is-0
andx2_i
is+0
, the result is+0
. - If
x1_i
is+0
andx2_i
is-0
, the result is+0
. - If
x1_i
is+0
andx2_i
is+0
, the result is+0
. - If
x1_i
is either+0
or-0
andx2_i
is a nonzero finite number, the result isx2_i
. - If
x1_i
is a nonzero finite number andx2_i
is either+0
or-0
, the result isx1_i
. - If
x1_i
is a nonzero finite number andx2_i
is-x1_i
, the result is+0
. - In the remaining cases, when neither
infinity
,+0
,-0
, nor aNaN
is involved, and the operands have the same mathematical sign or have different magnitudes, the sum must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported round mode. If the magnitude is too large to represent, the operation overflows and the result is aninfinity
of appropriate mathematical sign.
Floating-point addition is a commutative operation, but not always associative.
-
self: <array>
- array instance (augend array). Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- addend array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- addend array. Must be compatible with
-
out: <array>
- an array containing the element-wise sums. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise sums. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`add(x1, x2)`](elementwise_functions.md#addx1-x2-).
(method-and)=
Evaluates self_i & other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have an integer or boolean data type.
-
other: Union[ int, bool, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have an integer or boolean data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_and(x1, x2)`](elementwise_functions.md#logical_andx1-x2-).
(method-array_namespace)=
Returns an object that has all the array API functions on it.
-
self: <array>
- array instance.
-
api_version: <Optional[str]>
- string representing the version of the array API specification to be returned, in
'YYYY.MM'
form, for example,'2020.10'
. If it isNone
, 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
.
- string representing the version of the array API specification to be returned, in
-
out: <object>
- 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.
(method-bool)=
Converts a zero-dimensional boolean array to a Python bool
object.
-
self: <array>
- zero-dimensional array instance. Must have a boolean data type.
-
out: <bool>
- a Python
bool
object representing the single element of the array.
- a Python
(method-dlpack)=
Exports the array for consumption by {ref}function-from_dlpack
as a DLPack capsule.
-
self: <array>
- array instance.
-
stream: Optional[ Union[ int, Any ]]
-
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 a positive integer or-1
. Ifstream
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.For other device types which do have a stream, queue or similar synchronization 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-ordercl::sycl::queue
. This is allowed when libraries agree on such a convention, and may be standardized in a future version of this API standard.Support for a `stream` value other than `None` is optional and implementation-dependent.
Device-specific notes:
:::{admonition} CUDA
None
: producer must assume the legacy default stream (default).1
: the legacy default stream.2
: the per-thread default stream.> 2
: stream number represented as a Python integer.
0
is disallowed due to its ambiguity:0
could mean eitherNone
,1
, or2
. ::::::{admonition} ROCm
None
: producer must assume the legacy default stream (default).0
: the default stream.> 2
: stream number represented as a Python integer.
Using
1
and2
is not supported. :::It is recommended that implementers explicitly handle streams. If they use the legacy default stream, specifying `1` (CUDA) or `0` (ROCm) is preferred. `None` is a safe default for developers who do not want to think about stream handling at all, potentially at the cost of more synchronization than necessary.
-
-
capsule: <PyCapsule>
- a DLPack capsule for the array. See {ref}
data-interchange
for details.
- a DLPack capsule for the array. See {ref}
(method-dlpack_device)=
Returns device type and device ID in DLPack format. Meant for use within {ref}function-from_dlpack
.
-
self: <array>
- array instance.
-
device: Tuple[enum.IntEnum, int]
-
a tuple
(device_type, device_id)
in DLPack format. Valid device type enum members are:CPU = 1 CUDA = 2 CPU_PINNED = 3 OPENCL = 4 VULKAN = 7 METAL = 8 VPI = 9 ROCM = 10
-
(method-eq)=
Computes the truth value of self_i == other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. May have any data type.
-
other: Union[ int, float, bool, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). May have any data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
.
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`equal(x1, x2)`](elementwise_functions.md#equalx1-x2-).
(method-float)=
Converts a zero-dimensional floating-point array to a Python float
object.
-
self: <array>
- zero-dimensional array instance. Must have a floating-point data type.
-
out: <float>
- a Python
float
object representing the single element of the array instance.
- a Python
(method-floordiv)=
Evaluates self_i // other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`floor_divide(x1, x2)`](elementwise_functions.md#floor_dividex1-x2-).
(method-ge)=
Computes the truth value of self_i >= other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
.
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`greater_equal(x1, x2)`](elementwise_functions.md#greater_equalx1-x2-).
(method-getitem)=
Returns self[key]
.
-
self: <array>
- array instance.
-
key: Union[ int, slice, ellipsis, Tuple[ Union[ int, slice, ellipsis ], ... ], <array> ]
- index key.
-
out: <array>
- an array containing the accessed value(s). The returned array must have the same data type as
self
.
- an array containing the accessed value(s). The returned array must have the same data type as
(method-gt)=
Computes the truth value of self_i > other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
.
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`greater(x1, x2)`](elementwise_functions.md#greaterx1-x2-).
(method-index)=
Converts a zero-dimensional integer array to a Python int
object.
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/).
-
self: <array>
- zero-dimensional array instance. Must have an integer data type.
-
out: <int>
- a Python
int
object representing the single element of the array instance.
- a Python
(method-int)=
Converts a zero-dimensional integer array to a Python int
object.
-
self: <array>
- zero-dimensional array instance. Must have an integer data type.
-
out: <int>
- a Python
int
object representing the single element of the array instance.
- a Python
(method-invert)=
Evaluates ~self_i
for each element of an array instance.
-
self: <array>
- array instance. Should have an integer or boolean data type.
-
out: <array>
- an array containing the element-wise results. The returned array must have the same data type as
self
.
- an array containing the element-wise results. The returned array must have the same data type as
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_invert(x)`](elementwise_functions.md#bitwise_invertx-).
(method-le)=
Computes the truth value of self_i <= other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
.
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`less_equal(x1, x2)`](elementwise_functions.md#less_equalx1-x2-).
(method-lshift)=
Evaluates self_i << other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have an integer data type.
-
other: Union[ int, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have an integer data type. Each element must be greater than or equal to0
.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have the same data type as
self
.
- an array containing the element-wise results. The returned array must have the same data type as
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_left_shift(x1, x2)`](elementwise_functions.md#bitwise_left_shiftx1-x2-).
(method-lt)=
Computes the truth value of self_i < other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
.
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`less(x1, x2)`](elementwise_functions.md#lessx1-x2-).
(method-matmul)=
Computes the matrix product.
The `matmul` function must implement the same semantics as the built-in `@` operator (see [PEP 465](https://www.python.org/dev/peps/pep-0465)).
-
self: <array>
- array instance. Should have a numeric data type. Must have at least one dimension. If
self
is one-dimensional having shape(M)
andother
has more than one dimension,self
must be promoted to a two-dimensional array by prepending1
to its dimensions (i.e., must have shape(1, M)
). After matrix multiplication, the prepended dimensions in the returned array must be removed. Ifself
has more than one dimension (including after vector-to-matrix promotion),self
must be compatible withother
(see {ref}broadcasting
). Ifself
has shape(..., M, K)
, the innermost two dimensions form matrices on which to perform matrix multiplication.
- array instance. Should have a numeric data type. Must have at least one dimension. If
-
other: <array>
- other array. Should have a numeric data type. Must have at least one dimension. If
other
is one-dimensional having shape(N)
andself
has more than one dimension,other
must be promoted to a two-dimensional array by appending1
to its dimensions (i.e., must have shape(N, 1)
). After matrix multiplication, the appended dimensions in the returned array must be removed. Ifother
has more than one dimension (including after vector-to-matrix promotion),other
must be compatible withself
(see {ref}broadcasting
). Ifother
has shape(..., K, N)
, the innermost two dimensions form matrices on which to perform matrix multiplication.
- other array. Should have a numeric data type. Must have at least one dimension. If
-
out: <array>
- if both
self
andother
are one-dimensional arrays having shape(N)
, a zero-dimensional array containing the inner product as its only element. - if
self
is a two-dimensional array having shape(M, K)
andother
is a two-dimensional array having shape(K, N)
, a two-dimensional array containing the conventional matrix product and having shape(M, N)
. - if
self
is a one-dimensional array having shape(K)
andother
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. - if
self
is an array having shape(..., M, K)
andother
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. - if
self
is a two-dimensional array having shape(M, K)
andother
is an array having shape(..., K, N)
, an array having shape(..., M, N)
and containing the conventional matrix product for each stacked matrix. - if
self
is an array having shape(..., M, K)
andother
is a two-dimensional array having shape(K, N)
, an array having shape(..., M, N)
and containing the conventional matrix product for each stacked matrix. - if either
self
orother
has more than two dimensions, an array having a shape determined by {ref}broadcasting
self
againstother
and containing the conventional matrix product for each stacked matrix.
The returned array must have a data type determined by {ref}
type-promotion
.Results must equal the results returned by the equivalent function [`matmul(x1, x2)`](linear_algebra_functions.md#matmulx1-x2-).
- if both
- if either
self
orother
is a zero-dimensional array. - if
self
is a one-dimensional array having shape(N)
,other
is a one-dimensional array having shape(M)
, andN != M
. - if
self
is an array having shape(..., M, K)
,other
is an array having shape(..., L, N)
, andK != L
.
(method-mod)=
Evaluates self_i % other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- 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 floating-point data type determined by {ref}type-promotion
.
- an array containing the element-wise results. Each element-wise result must have the same sign as the respective element
Element-wise results must equal the results returned by the equivalent element-wise function [`remainder(x1, x2)`](elementwise_functions.md#remainderx1-x2-).
(method-mul)=
Calculates the product for each element of an array instance with the respective element of the array other
.
For floating-point operands, let self
equal x1
and other
equal x2
.
- If either
x1_i
orx2_i
isNaN
, the result isNaN
. - If
x1_i
is either+infinity
or-infinity
andx2_i
is either+0
or-0
, the result isNaN
. - If
x1_i
is either+0
or-0
andx2_i
is either+infinity
or-infinity
, the result isNaN
. - If
x1_i
andx2_i
have the same mathematical sign, the result has a positive mathematical sign, unless the result isNaN
. If the result isNaN
, the "sign" ofNaN
is implementation-defined. - If
x1_i
andx2_i
have different mathematical signs, the result has a negative mathematical sign, unless the result isNaN
. If the result isNaN
, the "sign" ofNaN
is implementation-defined. - If
x1_i
is either+infinity
or-infinity
andx2_i
is either+infinity
or-infinity
, the result is a signed infinity with the mathematical sign determined by the rule already stated above. - If
x1_i
is either+infinity
or-infinity
andx2_i
is a nonzero finite number, the result is a signed infinity with the mathematical sign determined by the rule already stated above. - If
x1_i
is a nonzero finite number andx2_i
is either+infinity
or-infinity
, the result is a signed infinity with the mathematical sign determined by the rule already stated above. - In the remaining cases, where neither
infinity
norNaN
is involved, the product must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too large to represent, the result is aninfinity
of appropriate mathematical sign. If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.
Floating-point multiplication is not always associative due to finite precision.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise products. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise products. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`multiply(x1, x2)`](elementwise_functions.md#multiplyx1-x2-).
(method-ne)=
Computes the truth value of self_i != other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. May have any data type.
-
other: Union[ int, float, bool, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). May have any data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type of
bool
(i.e., must be a boolean array).
- an array containing the element-wise results. The returned array must have a data type of
Element-wise results must equal the results returned by the equivalent element-wise function [`not_equal(x1, x2)`](elementwise_functions.md#not_equalx1-x2-).
(method-neg)=
Evaluates -self_i
for each element of an array instance.
For signed integer data types, the numerical negative of the minimum representable integer is implementation-dependent.
-
self: <array>
- array instance. Should have a numeric data type.
-
out: <array>
- an array containing the evaluated result for each element in
self
. The returned array must have a data type determined by {ref}type-promotion
.
- an array containing the evaluated result for each element in
Element-wise results must equal the results returned by the equivalent element-wise function [`negative(x)`](elementwise_functions.md#negativex-).
(method-or)=
Evaluates self_i | other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have an integer or boolean data type.
-
other: Union[ int, bool, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have an integer or boolean data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_or(x1, x2)`](elementwise_functions.md#bitwise_orx1-x2-).
(method-pos)=
Evaluates +self_i
for each element of an array instance.
-
self: <array>
- array instance. Should have a numeric data type.
-
out: <array>
- an array containing the evaluated result for each element. The returned array must have the same data type as
self
.
- an array containing the evaluated result for each element. The returned array must have the same data type as
Element-wise results must equal the results returned by the equivalent element-wise function [`positive(x)`](elementwise_functions.md#positivex-).
(method-pow)=
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
.
For floating-point operands, let self
equal x1
and other
equal x2
.
- If
x1_i
is not equal to1
andx2_i
isNaN
, the result isNaN
. - If
x2_i
is+0
, the result is1
, even ifx1_i
isNaN
. - If
x2_i
is-0
, the result is1
, even ifx1_i
isNaN
. - If
x1_i
isNaN
andx2_i
is not equal to0
, the result isNaN
. - If
abs(x1_i)
is greater than1
andx2_i
is+infinity
, the result is+infinity
. - If
abs(x1_i)
is greater than1
andx2_i
is-infinity
, the result is+0
. - If
abs(x1_i)
is1
andx2_i
is+infinity
, the result is1
. - If
abs(x1_i)
is1
andx2_i
is-infinity
, the result is1
. - If
x1_i
is1
andx2_i
is notNaN
, the result is1
. - If
abs(x1_i)
is less than1
andx2_i
is+infinity
, the result is+0
. - If
abs(x1_i)
is less than1
andx2_i
is-infinity
, the result is+infinity
. - If
x1_i
is+infinity
andx2_i
is greater than0
, the result is+infinity
. - If
x1_i
is+infinity
andx2_i
is less than0
, the result is+0
. - If
x1_i
is-infinity
andx2_i
is greater than0
, the result is-infinity
. - If
x1_i
is-infinity
,x2_i
is greater than0
, andx2_i
is not an odd integer value, the result is+infinity
. - If
x1_i
is-infinity
,x2_i
is less than0
, andx2_i
is an odd integer value, the result is-0
. - If
x1_i
is-infinity
,x2_i
is less than0
, andx2_i
is not an odd integer value, the result is+0
. - If
x1_i
is+0
andx2_i
is greater than0
, the result is+0
. - If
x1_i
is+0
andx2_i
is less than0
, the result is+infinity
. - If
x1_i
is-0
,x2_i
is greater than0
, andx2_i
is an odd integer value, the result is-0
. - If
x1_i
is-0
,x2_i
is greater than0
, andx2_i
is not an odd integer value, the result is+0
. - If
x1_i
is-0
,x2_i
is less than0
, andx2_i
is an odd integer value, the result is-infinity
. - If
x1_i
is-0
,x2_i
is less than0
, andx2_i
is not an odd integer value, the result is+infinity
. - If
x1_i
is less than0
,x1_i
is a finite number,x2_i
is a finite number, andx2_i
is not an integer value, the result isNaN
.
-
self: <array>
- array instance whose elements correspond to the exponentiation base. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array whose elements correspond to the exponentiation exponent. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array whose elements correspond to the exponentiation exponent. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`pow(x1, x2)`](elementwise_functions.md#powx1-x2-).
(method-rshift)=
Evaluates self_i >> other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have an integer data type.
-
other: Union[ int, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have an integer data type. Each element must be greater than or equal to0
.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have the same data type as
self
.
- an array containing the element-wise results. The returned array must have the same data type as
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_right_shift(x1, x2)`](elementwise_functions.md#bitwise_right_shiftx1-x2-).
(method-setitem)=
Sets self[key]
to value
.
-
self: <array>
- array instance.
-
key: Union[ int, slice, ellipsis, Tuple[ Union[ int, slice, ellipsis ], ... ], <array> ]
- index key.
-
value: Union[ int, float, bool, <array> ]
- value(s) to set. Must be compatible with
self[key]
(see {ref}broadcasting
).
- value(s) to set. Must be compatible with
Setting array values must not affect the data type of `self`.
When `value` is a Python scalar (i.e., `int`, `float`, `bool`), behavior must follow specification guidance on mixing arrays with Python scalars (see {ref}`type-promotion`).
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.
(method-sub)=
Calculates the difference for each element of an array instance with the respective element of the array other
. 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 __add__()
).
-
self: <array>
- array instance (minuend array). Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- subtrahend array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- subtrahend array. Must be compatible with
-
out: <array>
- an array containing the element-wise differences. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise differences. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`subtract(x1, x2)`](elementwise_functions.md#subtractx1-x2-).
(method-truediv)=
Evaluates self_i / other_i
for each element of an array instance with the respective element of the array other
.
For floating-point operands, let self
equal x1
and other
equal x2
.
- If either
x1_i
orx2_i
isNaN
, the result isNaN
. - If
x1_i
is either+infinity
or-infinity
andx2_i
is either+infinity
or-infinity
, the result isNaN
. - If
x1_i
is either+0
or-0
andx2_i
is either+0
or-0
, the result isNaN
. - If
x1_i
is+0
andx2_i
is greater than0
, the result is+0
. - If
x1_i
is-0
andx2_i
is greater than0
, the result-0
. - If
x1_i
is+0
andx2_i
is less than0
, the result is-0
. - If
x1_i
is-0
andx2_i
is less than0
, the result is+0
. - If
x1_i
is greater than0
andx2_i
is+0
, the result is+infinity
. - If
x1_i
is greater than0
andx2_i
is-0
, the result is-infinity
. - If
x1_i
is less than0
andx2_i
is+0
, the result is-infinity
. - If
x1_i
is less than0
andx2_i
is-0
, the result is+infinity
. - If
x1_i
is+infinity
andx2_i
is a positive (i.e., greater than0
) finite number, the result is+infinity
. - If
x1_i
is+infinity
andx2_i
is a negative (i.e., less than0
) finite number, the result is-infinity
. - If
x1_i
is-infinity
andx2_i
is a positive (i.e., greater than0
) finite number, the result is-infinity
. - If
x1_i
is-infinity
andx2_i
is a negative (i.e., less than0
) finite number, the result is+infinity
. - If
x1_i
is a positive (i.e., greater than0
) finite number andx2_i
is+infinity
, the result is+0
. - If
x1_i
is a positive (i.e., greater than0
) finite number andx2_i
is-infinity
, the result is-0
. - If
x1_i
is a negative (i.e., less than0
) finite number andx2_i
is+infinity
, the result is-0
. - If
x1_i
is a negative (i.e., less than0
) finite number andx2_i
is-infinity
, the result is+0
. - If
x1_i
andx2_i
have the same mathematical sign and are both nonzero finite numbers, the result has a positive mathematical sign. - If
x1_i
andx2_i
have different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign. - In the remaining cases, where neither
-infinity
,+0
,-0
, norNaN
is involved, the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too larger to represent, the operation overflows and the result is aninfinity
of appropriate mathematical sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
-
self: <array>
- array instance. Should have a numeric data type.
-
other: Union[ int, float, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have a numeric data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`divide(x1, x2)`](elementwise_functions.md#dividex1-x2-).
(method-xor)=
Evaluates self_i ^ other_i
for each element of an array instance with the respective element of the array other
.
-
self: <array>
- array instance. Should have an integer or boolean data type.
-
other: Union[ int, bool, <array> ]
- other array. Must be compatible with
self
(see {ref}broadcasting
). Should have an integer or boolean data type.
- other array. Must be compatible with
-
out: <array>
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
type-promotion
.
- an array containing the element-wise results. The returned array must have a data type determined by {ref}
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_xor(x1, x2)`](elementwise_functions.md#bitwise_xorx1-x2-).
(method-to_device)=
Copy the array from the device on which it currently resides to the specified device
.
-
self: <array>
- array instance.
-
device: <device>
- a
device
object (see {ref}device-support
).
- a
-
stream: Optional[ Union[ int, Any ]]
- stream object to use during copy. In addition to the types supported in {ref}
method-__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.
- stream object to use during copy. In addition to the types supported in {ref}
-
out: <array>
- an array with the same data and data type as
self
and located on the specifieddevice
.
- an array with the same data and data type as
If `stream` is given, 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 library's documentation.