class documentation

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().

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 to_device Undocumented
Property device Undocumented
Property dtype Array API compatible wrapper for np.ndarray.dtype.
Property mT 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.
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
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 _validate_index Validate an index according to the array API.
Class Variable _array Undocumented
def __abs__(self: Array, /) -> Array: (source)

Performs the operation __abs__.

def __add__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __add__.

def __and__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __and__.

def __array__(self, dtype: None|np.dtype[Any] = None) -> npt.NDArray[Any]: (source)

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.

def __array_namespace__(self: Array, /, *, api_version: Optional[str] = None) -> types.ModuleType: (source)

Undocumented

def __bool__(self: Array, /) -> bool: (source)

Performs the operation __bool__.

def __dlpack__(self: Array, /, *, stream: None = None) -> PyCapsule: (source)

Performs the operation __dlpack__.

def __dlpack_device__(self: Array, /) -> Tuple[IntEnum, int]: (source)

Performs the operation __dlpack_device__.

def __eq__(self: Array, other: Union[int, float, bool, Array], /) -> Array: (source)

Performs the operation __eq__.

def __float__(self: Array, /) -> float: (source)

Performs the operation __float__.

def __floordiv__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __floordiv__.

def __ge__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __ge__.

def __getitem__(self: Array, key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], Array], /) -> Array: (source)

Performs the operation __getitem__.

def __gt__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __gt__.

def __iadd__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __iadd__.

def __iand__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __iand__.

def __ifloordiv__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __ifloordiv__.

def __ilshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __ilshift__.

def __imatmul__(self: Array, other: Array, /) -> Array: (source)

Performs the operation __imatmul__.

def __imod__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __imod__.

def __imul__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __imul__.

def __index__(self: Array, /) -> int: (source)

Performs the operation __index__.

def __int__(self: Array, /) -> int: (source)

Performs the operation __int__.

def __invert__(self: Array, /) -> Array: (source)

Performs the operation __invert__.

def __ior__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __ior__.

def __ipow__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __ipow__.

def __irshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __irshift__.

def __isub__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __isub__.

def __itruediv__(self: Array, other: Union[float, Array], /) -> Array: (source)

Performs the operation __itruediv__.

def __ixor__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __ixor__.

def __le__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __le__.

def __lshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __lshift__.

def __lt__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __lt__.

def __matmul__(self: Array, other: Array, /) -> Array: (source)

Performs the operation __matmul__.

def __mod__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __mod__.

def __mul__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __mul__.

def __ne__(self: Array, other: Union[int, float, bool, Array], /) -> Array: (source)

Performs the operation __ne__.

def __neg__(self: Array, /) -> Array: (source)

Performs the operation __neg__.

def __new__(cls, *args, **kwargs): (source)

Undocumented

def __or__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __or__.

def __pos__(self: Array, /) -> Array: (source)

Performs the operation __pos__.

def __pow__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __pow__.

def __radd__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __radd__.

def __rand__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __rand__.

def __repr__(self: Array, /) -> str: (source)

Performs the operation __repr__.

def __rfloordiv__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __rfloordiv__.

def __rlshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __rlshift__.

def __rmatmul__(self: Array, other: Array, /) -> Array: (source)

Performs the operation __rmatmul__.

def __rmod__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __rmod__.

def __rmul__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __rmul__.

def __ror__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __ror__.

def __rpow__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __rpow__.

def __rrshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __rrshift__.

def __rshift__(self: Array, other: Union[int, Array], /) -> Array: (source)

Performs the operation __rshift__.

def __rsub__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __rsub__.

def __rtruediv__(self: Array, other: Union[float, Array], /) -> Array: (source)

Performs the operation __rtruediv__.

def __rxor__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __rxor__.

def __setitem__(self, key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], Array], value: Union[int, float, bool, Array], /): (source)

Performs the operation __setitem__.

def __str__(self: Array, /) -> str: (source)

Performs the operation __str__.

def __sub__(self: Array, other: Union[int, float, Array], /) -> Array: (source)

Performs the operation __sub__.

def __truediv__(self: Array, other: Union[float, Array], /) -> Array: (source)

Performs the operation __truediv__.

def __xor__(self: Array, other: Union[int, bool, Array], /) -> Array: (source)

Performs the operation __xor__.

def to_device(self: Array, device: Device, /, stream: None = None) -> Array: (source)

Undocumented

Undocumented

Array API compatible wrapper for np.ndarray.dtype.

See its docstring for more information.

Array API compatible wrapper for np.ndarray.ndim.

See its docstring for more information.

@property
shape: Tuple[int, ...] = (source)

Array API compatible wrapper for np.ndarray.shape.

See its docstring for more information.

Array API compatible wrapper for np.ndarray.size.

See its docstring for more information.

Array API compatible wrapper for np.ndarray.T.

See its docstring for more information.

@classmethod
def _new(cls, x, /): (source)

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) -> Tuple[Array, Array]: (source)

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.

def _check_allowed_dtypes(self, other: ((bool|int)|float)|Array, dtype_category: str, op: str) -> Array: (source)

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): (source)

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 _validate_index(self, key): (source)

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 raises IndexError if the index key is invalid. 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. This behaviour is sometimes referred to as flat indexing.
  • 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.

_array: np.ndarray = (source)

Undocumented