class documentation

class Array:

View In Hierarchy

n-d array object for the array API namespace.

See the docstring of np.ndarray for more information.

This is a wrapper around numpy.ndarray that restricts the usage to only those things that are required by the array API namespace. Note, attributes on this object that start with a single underscore are not part of the API specification and should only be used internally. This object should not be constructed directly. Rather, use one of the creation functions, such as asarray().

Class Method ​_new This is a private method for initializing the array API Array object.
Static Method ​_normalize​_two​_args Normalize inputs to two arg functions to fix type promotion rules
Static Method ​_validate​_index Validate an index according to the array API.
Method __abs__ Performs the operation __abs__.
Method __add__ Performs the operation __add__.
Method __and__ Performs the operation __and__.
Method __array__ Warning: this method is NOT part of the array API spec. Implementers of other libraries need not include it, and users should not assume it will be present in other implementations.
Method __array​_namespace__ Undocumented
Method __bool__ Performs the operation __bool__.
Method __dlpack__ Performs the operation __dlpack__.
Method __dlpack​_device__ Performs the operation __dlpack_device__.
Method __eq__ Performs the operation __eq__.
Method __float__ Performs the operation __float__.
Method __floordiv__ Performs the operation __floordiv__.
Method __ge__ Performs the operation __ge__.
Method __getitem__ Performs the operation __getitem__.
Method __gt__ Performs the operation __gt__.
Method __iadd__ Performs the operation __iadd__.
Method __iand__ Performs the operation __iand__.
Method __ifloordiv__ Performs the operation __ifloordiv__.
Method __ilshift__ Performs the operation __ilshift__.
Method __imatmul__ Performs the operation __imatmul__.
Method __imod__ Performs the operation __imod__.
Method __imul__ Performs the operation __imul__.
Method __index__ Performs the operation __index__.
Method __int__ Performs the operation __int__.
Method __invert__ Performs the operation __invert__.
Method __ior__ Performs the operation __ior__.
Method __ipow__ Performs the operation __ipow__.
Method __irshift__ Performs the operation __irshift__.
Method __isub__ Performs the operation __isub__.
Method __itruediv__ Performs the operation __itruediv__.
Method __ixor__ Performs the operation __ixor__.
Method __le__ Performs the operation __le__.
Method __lshift__ Performs the operation __lshift__.
Method __lt__ Performs the operation __lt__.
Method __matmul__ Performs the operation __matmul__.
Method __mod__ Performs the operation __mod__.
Method __mul__ Performs the operation __mul__.
Method __ne__ Performs the operation __ne__.
Method __neg__ Performs the operation __neg__.
Method __new__ Undocumented
Method __or__ Performs the operation __or__.
Method __pos__ Performs the operation __pos__.
Method __pow__ Performs the operation __pow__.
Method __radd__ Performs the operation __radd__.
Method __rand__ Performs the operation __rand__.
Method __repr__ Performs the operation __repr__.
Method __rfloordiv__ Performs the operation __rfloordiv__.
Method __rlshift__ Performs the operation __rlshift__.
Method __rmatmul__ Performs the operation __rmatmul__.
Method __rmod__ Performs the operation __rmod__.
Method __rmul__ Performs the operation __rmul__.
Method __ror__ Performs the operation __ror__.
Method __rpow__ Performs the operation __rpow__.
Method __rrshift__ Performs the operation __rrshift__.
Method __rshift__ Performs the operation __rshift__.
Method __rsub__ Performs the operation __rsub__.
Method __rtruediv__ Performs the operation __rtruediv__.
Method __rxor__ Performs the operation __rxor__.
Method __setitem__ Performs the operation __setitem__.
Method __str__ Performs the operation __str__.
Method __sub__ Performs the operation __sub__.
Method __truediv__ Performs the operation __truediv__.
Method __xor__ Performs the operation __xor__.
Method ​_check​_allowed​_dtypes Helper function for operators to only allow specific input dtypes
Method ​_promote​_scalar Returns a promoted version of a Python scalar appropriate for use with operations on self.
Method to​_device Undocumented
Property device Undocumented
Property dtype Array API compatible wrapper for np.ndarray.dtype.
Property m​T Undocumented
Property ndim Array API compatible wrapper for np.ndarray.ndim.
Property shape Array API compatible wrapper for np.ndarray.shape.
Property size Array API compatible wrapper for np.ndarray.size.
Property T Array API compatible wrapper for np.ndarray.T.
@classmethod
def _new(cls, x, /):

This is a private method for initializing the array API Array object.

Functions outside of the array_api submodule should not use this method. Use one of the creation functions instead, such as asarray.

@staticmethod
def _normalize_two_args(x1, x2):

Normalize inputs to two arg functions to fix type promotion rules

NumPy deviates from the spec type promotion rules in cases where one argument is 0-dimensional and the other is not. For example:

>>> import numpy as np
>>> a = np.array([1.0], dtype=np.float32)
>>> b = np.array(1.0, dtype=np.float64)
>>> np.add(a, b) # The spec says this should be float64
array([2.], dtype=float32)

To fix this, we add a dimension to the 0-dimension array before passing it through. This works because a dimension would be added anyway from broadcasting, so the resulting shape is the same, but this prevents NumPy from not promoting the dtype.

@staticmethod
def _validate_index(key, shape):

Validate an index according to the array API.

The array API specification only requires a subset of indices that are supported by NumPy. This function will reject any index that is allowed by NumPy but not required by the array API specification. We always raise IndexError on such indices (the spec does not require any specific behavior on them, but this makes the NumPy array API namespace a minimal implementation of the spec). See https://data-apis.org/array-api/latest/API_specification/indexing.html for the full list of required indexing behavior

This function either raises IndexError if the index key is invalid, or a new key to be used in place of key in indexing. It only raises IndexError on indices that are not already rejected by NumPy, as NumPy will already raise the appropriate error on such indices. shape may be None, in which case, only cases that are independent of the array shape are checked.

The following cases are allowed by NumPy, but not specified by the array API specification:

  • Indices to not include an implicit ellipsis at the end. That is, every axis of an array must be explicitly indexed or an ellipsis included.
  • The start and stop of a slice may not be out of bounds. In particular, for a slice i:j:k on an axis of size n, only the following are allowed:
    • i or j omitted (None).
    • -n <= i <= max(0, n - 1).
    • For k > 0 or k omitted (None), -n <= j <= n.
    • For k < 0, -n - 1 <= j <= max(0, n - 1).
  • Boolean array indices are not allowed as part of a larger tuple index.
  • Integer array indices are not allowed (with the exception of 0-D arrays, which are treated the same as scalars).

Additionally, it should be noted that indices that would return a scalar in NumPy will return a 0-D array. Array scalars are not allowed in the specification, only 0-D arrays. This is done in the Array._new constructor, not this function.

def __abs__(self, /):
Performs the operation __abs__.
Returns
ArrayUndocumented
def __add__(self, other, /):
Performs the operation __add__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __and__(self, other, /):
Performs the operation __and__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __array__(self, dtype=None):
Warning: this method is NOT part of the array API spec. Implementers of other libraries need not include it, and users should not assume it will be present in other implementations.
Parameters
dtype:None|np.dtype[Any]Undocumented
Returns
npt.NDArray[Any]Undocumented
def __array_namespace__(self, /, *, api_version=None):

Undocumented

Parameters
api​_version:Optional[str]Undocumented
Returns
types.ModuleTypeUndocumented
def __bool__(self, /):
Performs the operation __bool__.
Returns
boolUndocumented
def __dlpack__(self, /, *, stream=None):
Performs the operation __dlpack__.
Parameters
stream:NoneUndocumented
Returns
PyCapsuleUndocumented
def __dlpack_device__(self, /):
Performs the operation __dlpack_device__.
Returns
Tuple[IntEnum, int]Undocumented
def __eq__(self, other, /):
Performs the operation __eq__.
Parameters
other:Union[int, float, bool, Array]Undocumented
Returns
ArrayUndocumented
def __float__(self, /):
Performs the operation __float__.
Returns
floatUndocumented
def __floordiv__(self, other, /):
Performs the operation __floordiv__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __ge__(self, other, /):
Performs the operation __ge__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __getitem__(self, key, /):
Performs the operation __getitem__.
Parameters
key:Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], Array]Undocumented
Returns
ArrayUndocumented
def __gt__(self, other, /):
Performs the operation __gt__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __iadd__(self, other, /):
Performs the operation __iadd__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __iand__(self, other, /):
Performs the operation __iand__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __ifloordiv__(self, other, /):
Performs the operation __ifloordiv__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __ilshift__(self, other, /):
Performs the operation __ilshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __imatmul__(self, other, /):
Performs the operation __imatmul__.
Parameters
other:ArrayUndocumented
Returns
ArrayUndocumented
def __imod__(self, other, /):
Performs the operation __imod__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __imul__(self, other, /):
Performs the operation __imul__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __index__(self, /):
Performs the operation __index__.
Returns
intUndocumented
def __int__(self, /):
Performs the operation __int__.
Returns
intUndocumented
def __invert__(self, /):
Performs the operation __invert__.
Returns
ArrayUndocumented
def __ior__(self, other, /):
Performs the operation __ior__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __ipow__(self, other, /):
Performs the operation __ipow__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __irshift__(self, other, /):
Performs the operation __irshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __isub__(self, other, /):
Performs the operation __isub__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __itruediv__(self, other, /):
Performs the operation __itruediv__.
Parameters
other:Union[float, Array]Undocumented
Returns
ArrayUndocumented
def __ixor__(self, other, /):
Performs the operation __ixor__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __le__(self, other, /):
Performs the operation __le__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __lshift__(self, other, /):
Performs the operation __lshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __lt__(self, other, /):
Performs the operation __lt__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __matmul__(self, other, /):
Performs the operation __matmul__.
Parameters
other:ArrayUndocumented
Returns
ArrayUndocumented
def __mod__(self, other, /):
Performs the operation __mod__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __mul__(self, other, /):
Performs the operation __mul__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __ne__(self, other, /):
Performs the operation __ne__.
Parameters
other:Union[int, float, bool, Array]Undocumented
Returns
ArrayUndocumented
def __neg__(self, /):
Performs the operation __neg__.
Returns
ArrayUndocumented
def __new__(cls, *args, **kwargs):

Undocumented

def __or__(self, other, /):
Performs the operation __or__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __pos__(self, /):
Performs the operation __pos__.
Returns
ArrayUndocumented
def __pow__(self, other, /):
Performs the operation __pow__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __radd__(self, other, /):
Performs the operation __radd__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __rand__(self, other, /):
Performs the operation __rand__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __repr__(self, /):
Performs the operation __repr__.
Returns
strUndocumented
def __rfloordiv__(self, other, /):
Performs the operation __rfloordiv__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __rlshift__(self, other, /):
Performs the operation __rlshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __rmatmul__(self, other, /):
Performs the operation __rmatmul__.
Parameters
other:ArrayUndocumented
Returns
ArrayUndocumented
def __rmod__(self, other, /):
Performs the operation __rmod__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __rmul__(self, other, /):
Performs the operation __rmul__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __ror__(self, other, /):
Performs the operation __ror__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __rpow__(self, other, /):
Performs the operation __rpow__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __rrshift__(self, other, /):
Performs the operation __rrshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __rshift__(self, other, /):
Performs the operation __rshift__.
Parameters
other:Union[int, Array]Undocumented
Returns
ArrayUndocumented
def __rsub__(self, other, /):
Performs the operation __rsub__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __rtruediv__(self, other, /):
Performs the operation __rtruediv__.
Parameters
other:Union[float, Array]Undocumented
Returns
ArrayUndocumented
def __rxor__(self, other, /):
Performs the operation __rxor__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def __setitem__(self, key, value, /):
Performs the operation __setitem__.
Parameters
key:Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], Array]Undocumented
value:Union[int, float, bool, Array]Undocumented
def __str__(self, /):
Performs the operation __str__.
Returns
strUndocumented
def __sub__(self, other, /):
Performs the operation __sub__.
Parameters
other:Union[int, float, Array]Undocumented
Returns
ArrayUndocumented
def __truediv__(self, other, /):
Performs the operation __truediv__.
Parameters
other:Union[float, Array]Undocumented
Returns
ArrayUndocumented
def __xor__(self, other, /):
Performs the operation __xor__.
Parameters
other:Union[int, bool, Array]Undocumented
Returns
ArrayUndocumented
def _check_allowed_dtypes(self, other, dtype_category, op):

Helper function for operators to only allow specific input dtypes

Use like

other = self._check_allowed_dtypes(other, 'numeric', '__add__') if other is NotImplemented:

return other
def _promote_scalar(self, scalar):

Returns a promoted version of a Python scalar appropriate for use with operations on self.

This may raise an OverflowError in cases where the scalar is an integer that is too large to fit in a NumPy integer dtype, or TypeError when the scalar type is incompatible with the dtype of self.

def to_device(self, device, /, stream=None):

Undocumented

Parameters
device:DeviceUndocumented
stream:NoneUndocumented
Returns
ArrayUndocumented
@property
device: Device =

Undocumented

@property
dtype: Dtype =

Array API compatible wrapper for np.ndarray.dtype.

See its docstring for more information.

@property
mT: Array =

Undocumented

@property
ndim: int =

Array API compatible wrapper for np.ndarray.ndim.

See its docstring for more information.

@property
shape: Tuple[int, ...] =

Array API compatible wrapper for np.ndarray.shape.

See its docstring for more information.

@property
size: int =

Array API compatible wrapper for np.ndarray.size.

See its docstring for more information.

@property
T: Array =

Array API compatible wrapper for np.ndarray.T.

See its docstring for more information.