|
| 1 | +# Manipulation Functions |
| 2 | + |
| 3 | +> Array API specification for manipulating arrays. |
| 4 | +
|
| 5 | +A conforming implementation of the array API standard must provide and support the following functions adhering to the following conventions. |
| 6 | + |
| 7 | +- Positional parameters must be [positional-only](https://www.python.org/dev/peps/pep-0570/) 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. |
| 8 | +- Optional parameters must be [keyword-only](https://www.python.org/dev/peps/pep-3102/) arguments. |
| 9 | +- Unless stated otherwise, functions must adhere to the type promotion rules defined in :ref:`type-promotion`. |
| 10 | + |
| 11 | +<!-- NOTE: please keep the functions in alphabetical order --> |
| 12 | + |
| 13 | +### <a name="concat" href="#concat">#</a> concat(arrays, /, *, axis=0) |
| 14 | + |
| 15 | +Joins a sequence of arrays along an existing axis. |
| 16 | + |
| 17 | +#### Parameters |
| 18 | + |
| 19 | +- **arrays**: _Tuple\[ <array> ]_ |
| 20 | + |
| 21 | + - input arrays to join. The arrays must have the same shape, except in the dimension specified by `axis`. |
| 22 | + |
| 23 | +- **axis**: _Optional\[ int ]_ |
| 24 | + |
| 25 | + - axis along which the arrays will be joined. If `axis` is `None`, arrays must be flattened before concatenation. If `axis` is negative, the function must determine the axis along which to join by counting from the last dimension. Default: `0`. |
| 26 | + |
| 27 | +#### Returns |
| 28 | + |
| 29 | +- **out**: _<array>_ |
| 30 | + |
| 31 | + - an output array containing the concatenated values. If the input arrays have different data types, normal [type promotion rules](type_promotion.md) apply. If the input arrays have the same data type, the output array must have the same data type as the input arrays. |
| 32 | + |
| 33 | + .. note:: |
| 34 | + |
| 35 | + This specification leaves type promotion between data type families (i.e., `intxx` and `floatxx`) unspecified. |
| 36 | + |
| 37 | +### <a name="expand_dims" href="#expand_dims">#</a> expand_dims(x, axis, /) |
| 38 | + |
| 39 | +Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by `axis`. |
| 40 | + |
| 41 | +#### Parameters |
| 42 | + |
| 43 | +- **x**: _<array>_ |
| 44 | + |
| 45 | + - input array. |
| 46 | + |
| 47 | +- **axis**: _int_ |
| 48 | + |
| 49 | + - axis position. Must follow Python's indexing rules: zero-based and negative indices must be counted backward from the last dimension. If `x` has rank `N`, a valid `axis` must reside on the interval `[-N-1, N+1]`. An `IndexError` exception must be raised if provided an invalid `axis` position. |
| 50 | + |
| 51 | +#### Returns |
| 52 | + |
| 53 | +- **out**: _<array>_ |
| 54 | + |
| 55 | + - an expanded output array having the same data type and shape as `x`. |
| 56 | + |
| 57 | +### <a name="flip" href="#flip">#</a> flip(x, /, *, axis=None) |
| 58 | + |
| 59 | +Reverses the order of elements in an array along the given axis. The shape of the array must be preserved. |
| 60 | + |
| 61 | +#### Parameters |
| 62 | + |
| 63 | +- **x**: _<array>_ |
| 64 | + |
| 65 | + - input array. |
| 66 | + |
| 67 | +- **axis**: _Optional\[ Union\[ int, Tuple\[ int, ... ] ] ]_ |
| 68 | + |
| 69 | + - axis (or axes) along which to flip. If `axis` is `None`, the function must flip all input array axes. If `axis` is negative, the function must count from the last dimension. If provided more than one axis, the function must flip only the specified axes. Default: `None`. |
| 70 | + |
| 71 | +#### Returns |
| 72 | + |
| 73 | +- **out**: _<array>_ |
| 74 | + |
| 75 | + - an output array having the same data type and shape as `x` and whose elements, relative to `x`, are reordered. |
| 76 | + |
| 77 | +### <a name="reshape" href="#reshape">#</a> reshape(x, shape, /) |
| 78 | + |
| 79 | +Reshapes an array without changing its data. |
| 80 | + |
| 81 | +#### Parameters |
| 82 | + |
| 83 | +- **x**: _<array>_ |
| 84 | + |
| 85 | + - input array to reshape. |
| 86 | + |
| 87 | +- **shape**: _Tuple\[ int, ... ]_ |
| 88 | + |
| 89 | + - a new shape compatible with the original shape. One shape dimension is allowed to be `-1`. When a shape dimension is `-1`, the corresponding output array shape dimension must be inferred from the length of the array and the remaining dimensions. |
| 90 | + |
| 91 | +#### Returns |
| 92 | + |
| 93 | +- **out**: _<array>_ |
| 94 | + |
| 95 | + - an output array having the same data type, elements, and underlying element order as `x`. |
| 96 | + |
| 97 | +### <a name="roll" href="#roll">#</a> roll(x, shift, /, *, axis=None) |
| 98 | + |
| 99 | +Rolls array elements along a specified axis. Array elements that roll beyond the last position are re-introduced at the first position. Array elements that roll beyond the first position are re-introduced at the last position. |
| 100 | + |
| 101 | +#### Parameters |
| 102 | + |
| 103 | +- **x**: _<array>_ |
| 104 | + |
| 105 | + - input array. |
| 106 | + |
| 107 | +- **shift**: _Union\[ int, Tuple\[ int, ... ] ]_ |
| 108 | + |
| 109 | + - number of places by which the elements are shifted. If `shift` is a tuple, then `axis` must be a tuple of the same size, and each of the given axes must be shifted by the corresponding element in `shift`. If `shift` is an `int` and `axis` a tuple, then the same `shift` is used for all specified axes. If a shift is positive, then array elements are shifted positively (toward larger indices) along the dimension of `axis`. If a shift is negative, then array elements are shifted negatively (toward smaller indices) along the dimension of `axis`. |
| 110 | + |
| 111 | +- **axis**: _Optional\[ Union\[ int, Tuple\[ int, ... ] ] ]_ |
| 112 | + |
| 113 | + - axis (or axes) along which elements to shift. If `axis` is `None`, the array is flattened, shifted, and then restored to its original shape. Default: `None`. |
| 114 | + |
| 115 | +#### Returns |
| 116 | + |
| 117 | +- **out**: _<array>_ |
| 118 | + |
| 119 | + - an output array having the same data type as `x` and whose elements, relative to `x`, are shifted. |
| 120 | + |
| 121 | +### <a name="squeeze" href="#squeeze">#</a> squeeze(x, /, *, axis=None) |
| 122 | + |
| 123 | +Removes singleton dimensions (axes) from `x`. |
| 124 | + |
| 125 | +#### Parameters |
| 126 | + |
| 127 | +- **x**: _<array>_ |
| 128 | + |
| 129 | + - input array. |
| 130 | + |
| 131 | +- **axis**: _Optional\[ Union\[ int, Tuple\[ int, ... ] ] ]_ |
| 132 | + |
| 133 | + - axis (or axes) to squeeze. If provided, only the specified axes must be squeezed. If `axis` is `None`, all singleton dimensions (axes) must be removed. If a specified axis has a size greater than one, the specified axis must be left unchanged. Default: `None`. |
| 134 | + |
| 135 | +#### Returns |
| 136 | + |
| 137 | +- **out**: _<array>_ |
| 138 | + |
| 139 | + - an output array having the same data type and elements as `x`. |
| 140 | + |
| 141 | +### <a name="stack" href="#stack">#</a> stack(arrays, /, *, axis=0) |
| 142 | + |
| 143 | +Joins a sequence of arrays along a new axis. |
| 144 | + |
| 145 | +#### Parameters |
| 146 | + |
| 147 | +- **arrays**: _Tuple\[ <array> ]_ |
| 148 | + |
| 149 | + - input arrays to join. Each array must have the same shape. |
| 150 | + |
| 151 | +- **axis**: _Optional\[ int ]_ |
| 152 | + |
| 153 | + - axis along which the arrays will be joined. Providing an `axis` specifies the index of the new axis in the dimensions of the result. For example, if `axis` is `0`, the new axis will be the first dimension and the output array will have shape `(N, A, B, C)`; if `axis` is `1`, the new axis will be the second dimension and the output array will have shape `(A, N, B, C)`; and, if `axis` is `-1`, the new axis will be the last dimension and the output array will have shape `(A, B, C, N)`. A valid `axis` must be on the interval `[-N, N)`, where `N` is the rank (number of dimensions) of `x`. If provided an `axis` outside of the required interval, the function must raise an exception. Default: `0`. |
| 154 | + |
| 155 | +#### Returns |
| 156 | + |
| 157 | +- **out**: _<array>_ |
| 158 | + |
| 159 | + - an output array having rank `N+1`, where `N` is the rank (number of dimensions) of `x`. If the input arrays have different data types, normal [type promotion rules](type_promotion.md) apply. If the input arrays have the same data type, the output array must have the same data type as the input arrays. |
| 160 | + |
| 161 | + .. note:: |
| 162 | + |
| 163 | + This specification leaves type promotion between data type families (i.e., `intxx` and `floatxx`) unspecified. |
0 commit comments