Array API specification for creating arrays.
A conforming implementation of the array API standard must provide and support the following functions adhering to the following conventions.
- Positional parameters must be positional-only parameters. Positional-only parameters have no externally-usable name. When a function 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.
(function-arange)=
Returns evenly spaced values within the half-open interval [start, stop)
as a one-dimensional array.
-
start: Union[ int, float ]
- if
stop
is specified, the start of interval (inclusive); otherwise, the end of the interval (exclusive). Ifstop
is not specified, the default starting value is0
.
- if
-
stop: Optional[ Union[ int, float ] ]
- the end of the interval. Default:
None
.
- the end of the interval. Default:
This function cannot guarantee that the interval does not include the `stop` value in those cases where `step` is not an integer and floating-point rounding errors affect the length of the output array.
-
step: Union[ int, float ]
- the distance between two adjacent elements (
out[i+1] - out[i]
). Default:1
.
- the distance between two adjacent elements (
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- a one-dimensional array containing evenly spaced values. The length of the output array must be
ceil((stop-start)/step)
.
- a one-dimensional array containing evenly spaced values. The length of the output array must be
(function-asarray)=
Convert the input to an array.
-
obj: Union[ float, NestedSequence[ bool | int | float ], SupportsDLPack, SupportsBufferProtocol ]
- Object to be converted to an array. Can be a Python scalar, a (possibly nested) sequence of Python scalars, or an object supporting DLPack or the Python buffer protocol.
:::{tip} An object supporting DLPack has
__dlpack__
and__dlpack_device__
methods. An object supporting the buffer protocol can be turned into a memoryview throughmemoryview(obj)
. ::: -
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be inferred from the data type(s) inobj
. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
copy: Optional[ bool ]
- Whether or not to make a copy of the input. If
True
, always copies. IfFalse
, never copies for input which supports DLPack or the buffer protocol, and raisesValueError
in case that would be necessary. IfNone
, reuses existing memory buffer if possible, copies otherwise. Default:None
.
- Whether or not to make a copy of the input. If
-
out: <array>
- An array containing the data from
obj
.
- An array containing the data from
(function-empty)=
Returns an uninitialized array having a specified shape
.
-
shape: Union[ int, Tuple[ int, ... ] ]
- output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- an array containing uninitialized data.
(function-empty_like)=
Returns an uninitialized array with the same shape
as an input array x
.
-
x: <array>
- input array from which to derive the output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be inferred fromx
. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. If
device
isNone
, the default device must be used, notx.device
. Default:None
.
- device to place the created array on, if given. If
-
out: <array>
- an array having the same shape as
x
and containing uninitialized data.
- an array having the same shape as
(function-eye)=
Returns a two-dimensional array with ones on the k
th diagonal and zeros elsewhere.
-
N: int
- number of rows in the output array.
-
M: Optional[ int ]
- number of columns in the output array. If
None
, the default number of columns in the output array isN
. Default:None
.
- number of columns in the output array. If
-
k: Optional[ int ]
- index of the diagonal. A positive value refers to an upper diagonal, a negative value to a lower diagonal, and
0
to the main diagonal. Default:0
.
- index of the diagonal. A positive value refers to an upper diagonal, a negative value to a lower diagonal, and
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- an array where all elements are equal to zero, except for the
k
th diagonal, whose values are equal to one.
- an array where all elements are equal to zero, except for the
(function-from_dlpack)=
Returns a new array containing the data from another (array) object with a __dlpack__
method.
-
x: object
- input (array) object.
-
out: <array>
-
an array containing the data in
x
.The returned array may be either a copy or a view. See {ref}`data-interchange` for details.
-
(function-full)=
Returns a new array having a specified shape
and filled with fill_value
.
-
shape: Union[ int, Tuple[ int, ... ] ]
- output array shape.
-
fill_value: Union[ int, float ]
- fill value.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- an array where every element is equal to
fill_value
.
- an array where every element is equal to
(function-full_like)=
Returns a new array filled with fill_value
and having the same shape
as an input array x
.
-
x: <array>
- input array from which to derive the output array shape.
-
fill_value: Union[ int, float ]
- fill value.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be inferred fromx
. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. If
device
isNone
, the default device must be used, notx.device
. Default:None
.
- device to place the created array on, if given. If
-
out: <array>
- an array having the same shape as
x
and where every element is equal tofill_value
.
- an array having the same shape as
(function-linspace)=
Returns evenly spaced numbers over a specified interval.
-
start: Union[ int, float ]
- the start of the interval.
-
stop: Union[ int, float ]
-
the end of the interval. If
endpoint
isFalse
, the function must generate a sequence ofnum+1
evenly spaced numbers starting withstart
and ending withstop
and exclude thestop
from the returned array such that the returned array consists of evenly spaced numbers over the half-open interval[start, stop)
. Ifendpoint
isTrue
, the output array must consist of evenly spaced numbers over the closed interval[start, stop]
. Default:True
.The step size changes when `endpoint` is `False`.
-
-
num: int
- number of samples. Must be a non-negative integer value; otherwise, the function must raise an exception.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
endpoint: bool
- boolean indicating whether to include
stop
in the interval. Default:True
.
- boolean indicating whether to include
-
out: <array>
- a one-dimensional array containing evenly spaced values.
(function-meshgrid)=
Returns coordinate matrices from coordinate vectors.
-
For
n
one dimensional arraysx1, x2, ..., xn
with lengthsNi = len(xi)
, this function returns(N1, N2, N3, ..., Nn)
shaped arrays if the indexing is 'ij' or(N2, N1, N3, ..., Nn)
shaped arrays if indexing 'xy'. -
The 0-D and 1-D case, the indexing keyword has no effect.
-
*arrays: <array>
- One dimensional arrays representing the coordinates of the grid.
-
indexing: Optional[ str ]
- Cartesian 'xy' by default or 'ij' for matrix indexing of the output.
-
out: Tuple[ <array>, ... ]
- List of N arrays with rank
max(2, N)
.
- List of N arrays with rank
This function supports indexing conventions using the indexing
keyword. In the case of xy
the function will return a meshgrid with cartesian indexing, while ij
will use matrix indexing. The difference is illustrated by the following code snippet:
xv, yv = meshgrid(x, y, indexing='xy')
for i in range(nx):
for j in range(ny):
# treat xv[j, i], yv[j, i]
xv, yv = meshgrid(x, y, indexing='ij')
for i in range(nx):
for j in range(ny):
# treat xv[i, j], yv[i, j]
(function-ones)=
Returns a new array having a specified shape
and filled with ones.
-
shape: Union[ int, Tuple[ int, ... ] ]
- output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- an array containing ones.
(function-ones_like)=
Returns a new array filled with ones and having the same shape
as an input array x
.
-
x: <array>
- input array from which to derive the output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be inferred fromx
. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. If
device
isNone
, the default device must be used, notx.device
. Default:None
.
- device to place the created array on, if given. If
-
out: <array>
- an array having the same shape as
x
and filled with ones.
- an array having the same shape as
(function-zeros)=
Returns a new array having a specified shape
and filled with zeros.
-
shape: Union[ int, Tuple[ int, ... ] ]
- output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be the default floating-point data type. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. Default:
None
.
- device to place the created array on, if given. Default:
-
out: <array>
- an array containing zeros.
(function-zeros_like)=
Returns a new array filled with zeros and having the same shape
as an input array x
.
-
x: <array>
- input array from which to derive the output array shape.
-
dtype: Optional[ <dtype> ]
- output array data type. If
dtype
isNone
, the output array data type must be inferred fromx
. Default:None
.
- output array data type. If
-
device: Optional[ <device> ]
- device to place the created array on, if given. If
device
isNone
, the default device must be used, notx.device
. Default:None
.
- device to place the created array on, if given. If
-
out: <array>
- an array having the same shape as
x
and filled with zeros.
- an array having the same shape as