Movatterモバイル変換


[0]ホーム

URL:


Skip to content

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.

Furthermore, a conforming implementation of the array API standard must support, at minimum, array objects of rank (i.e., number of dimensions)0,1,2,3, and4 and must explicitly document their maximum supported rankN.

Note

Conforming implementations must support zero-dimensional arrays.

Apart from array object attributes, such asndim,device, anddtype, all operations in this standard return arrays (or tuples of arrays), including those operations, such asmean,var, andstd, from which some common array libraries (e.g., NumPy) return scalar values.

Rationale: always returning arrays is necessary to (1) support accelerator libraries where non-array return values could force device synchronization and (2) support delayed execution models where an array represents a future value.


Operators

A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators.

Arithmetic Operators

A conforming implementation of the array API standard must provide and support an array object supporting the following Python arithmetic operators.

Arithmetic operators should be defined for arrays having real-valued data types.

Array Operators

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.

Bitwise Operators

A conforming implementation of the array API standard must provide and support an array object supporting the following Python bitwise operators.

Bitwise operators should be defined for arrays having integer and boolean data types.

Comparison Operators

A conforming implementation of the array API standard must provide and support an array object supporting the following Python comparison operators.

array.__lt__(),array.__le__(),array.__gt__(),array.__ge__() are only defined for arrays having real-valued data types. Other comparison operators should be defined for arrays having any data type.For backward compatibility, conforming implementations may support complex numbers; however, inequality comparison of complex numbers is unspecified and thus implementation-dependent (seeComplex Number Ordering).

In-place Operators

Note

In-place operations must be supported as discussed inCopy-view behavior and mutability.

A conforming implementation of the array API standard must provide and support an array object supporting the following “in-place” Python operators.

Note

This specification refers to the following operators as “in-place” as that is what these operators are called inPython<https://docs.python.org/3/library/operator.html#in-place-operators>. However, conforming array libraries which do not support array mutation may choose to not explicitly implement in-place Python operators. When a library does not implement a method corresponding to an in-place Python operator, Python falls back to the equivalent method for the corresponding binary arithmetic operation.

An in-place operation must not change the data type or shape of the in-place array as a result ofType Promotion Rules orBroadcasting.

Letx1+=x2 be a representative in-place operation. If, after applying type promotion (seeType Promotion Rules) to in-place operandsx1 andx2, the resulting data type is equal to the data type of the array on the left-hand side of the operation (i.e.,x1), then an in-place operation must have the same behavior (including special cases) as the respective binary (i.e., two operand, non-assignment) operation. In this case, for the in-place additionx1+=x2, the modified arrayx1 must always equal the result of the equivalent binary arithmetic operationx1[...]=x1+x2.

If, however, after applying type promotion (seeType Promotion Rules) to in-place operands, the resulting data type is not equal to the data type of the array on the left-hand side of the operation, then a conforming implementation may return results which differ from the respective binary operation due to casting behavior and selection of the operation’s intermediate precision. The choice of casting behavior and intermediate precision is unspecified and thus implementation-defined.

Note

Letx1 be the operand on the left-hand side andx2 be the operand on the right-hand side of an in-place operation. Consumers of the array API standard are advised of the following considerations when using in-place operations:

  1. In-place operations do not guarantee in-place mutation. A conforming library may or may not support in-place mutation.

  2. If, after applying broadcasting (seeBroadcasting) to in-place operands, the resulting shape is not equal to the shape ofx1, in-place operators may raise an exception.

  3. If, after applying type promotion (seeType Promotion Rules) to in-place operands, the resulting data type is not equal to the data type ofx1, the resulting data type may not equal the data type ofx1 and the operation’s intermediate precision may be that ofx1, even if the promoted data type betweenx1 andx2 would have higher precision.

In general, for in-place operations, consumers of the array API standard are advised to ensure operands have the same data type and broadcast to the shape of the operand on the left-hand side of the operation in order to maximize portability.

Arithmetic Operators

  • +=. 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__.

Array Operators

  • @=. May be implemented via__imatmul__.

Bitwise Operators

  • &=. 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__.

Reflected Operators

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.

Note

All operators for whicharray<op>scalar is implemented must have an equivalent reflected operator implementation.

Arithmetic Operators

  • __radd__

  • __rsub__

  • __rmul__

  • __rtruediv__

  • __rfloordiv__

  • __rpow__

  • __rmod__

Array Operators

  • __rmatmul__

Bitwise Operators

  • __rand__

  • __ror__

  • __rxor__

  • __rlshift__

  • __rrshift__


Attributes

array.dtype

Data type of the array elements.

array.device

Hardware device the array data resides on.

array.mT

Transpose of a matrix (or a stack of matrices).

array.ndim

Number of array dimensions (axes).

array.shape

Array dimensions.

array.size

Number of elements in an array.

array.T

Transpose of the array.


Methods

array.__abs__()

Calculates the absolute value for each element of an array instance.

array.__add__(other, /)

Calculates the sum for each element of an array instance with the respective element of the arrayother.

array.__and__(other, /)

Evaluatesself_i&other_i for each element of an array instance with the respective element of the arrayother.

array.__array_namespace__(*, api_version=None)

Returns an object that has all the array API functions on it.

array.__bool__()

Converts a zero-dimensional array to a Pythonbool object.

array.__complex__()

Converts a zero-dimensional array to a Pythoncomplex object.

array.__dlpack__(*, stream=None, max_version=None, dl_device=None, copy=None)

Exports the array for consumption byfrom_dlpack() as a DLPack capsule.

array.__dlpack_device__()

Returns device type and device ID in DLPack format.

array.__eq__(other, /)

Computes the truth value ofself_i==other_i for each element of an array instance with the respective element of the arrayother.

array.__float__()

Converts a zero-dimensional array to a Pythonfloat object.

array.__floordiv__(other, /)

Evaluatesself_i//other_i for each element of an array instance with the respective element of the arrayother.

array.__ge__(other, /)

Computes the truth value ofself_i>=other_i for each element of an array instance with the respective element of the arrayother.

array.__getitem__(key, /)

Returnsself[key].

array.__gt__(other, /)

Computes the truth value ofself_i>other_i for each element of an array instance with the respective element of the arrayother.

array.__index__()

Converts a zero-dimensional integer array to a Pythonint object.

array.__int__()

Converts a zero-dimensional array to a Pythonint object.

array.__invert__()

Evaluates~self_i for each element of an array instance.

array.__le__(other, /)

Computes the truth value ofself_i<=other_i for each element of an array instance with the respective element of the arrayother.

array.__lshift__(other, /)

Evaluatesself_i<<other_i for each element of an array instance with the respective element of the arrayother.

array.__lt__(other, /)

Computes the truth value ofself_i<other_i for each element of an array instance with the respective element of the arrayother.

array.__matmul__(other, /)

Computes the matrix product.

array.__mod__(other, /)

Evaluatesself_i%other_i for each element of an array instance with the respective element of the arrayother.

array.__mul__(other, /)

Calculates the product for each element of an array instance with the respective element of the arrayother.

array.__ne__(other, /)

Computes the truth value ofself_i!=other_i for each element of an array instance with the respective element of the arrayother.

array.__neg__()

Evaluates-self_i for each element of an array instance.

array.__or__(other, /)

Evaluatesself_i|other_i for each element of an array instance with the respective element of the arrayother.

array.__pos__()

Evaluates+self_i for each element of an array instance.

array.__pow__(other, /)

Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power ofother_i (the exponent), whereother_i is the corresponding element of the arrayother.

array.__rshift__(other, /)

Evaluatesself_i>>other_i for each element of an array instance with the respective element of the arrayother.

array.__setitem__(key, value, /)

Setsself[key] tovalue.

array.__sub__(other, /)

Calculates the difference for each element of an array instance with the respective element of the arrayother.

array.__truediv__(other, /)

Evaluatesself_i/other_i for each element of an array instance with the respective element of the arrayother.

array.__xor__(other, /)

Evaluatesself_i^other_i for each element of an array instance with the respective element of the arrayother.

array.to_device(device, /, *, stream=None)

Copy the array from the device on which it currently resides to the specifieddevice.


[8]ページ先頭

©2009-2025 Movatter.jp