Skip to content

Commit 5a5874c

Browse files
committed
feat: add support for specifying a tuple of axis positions
Closes: #760
1 parent d9f2a7a commit 5a5874c

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/array_api_stubs/_draft/manipulation_functions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,28 @@ def concat(
7979
"""
8080

8181

82-
def expand_dims(x: array, /, axis: int) -> array:
82+
def expand_dims(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array:
8383
"""
84-
Expands the shape of an array by inserting a new axis of size one at the position specified by ``axis``.
84+
Expands the shape of an array by inserting a new axis of size one at the position (or positions) specified by ``axis``.
8585
8686
Parameters
8787
----------
8888
x: array
8989
input array.
90-
axis: int
91-
axis position (zero-based). A valid ``axis`` **must** reside on the closed-interval ``[-N-1, N]``, where ``N`` is the number of axes in ``x``. If an axis is specified as a negative integer, the axis position at which to insert a singleton dimension **must** be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved axis position **must** be ``N`` (i.e., a singleton dimension **must** be appended to the input array ``x``). If provided ``-N-1``, the resolved axis position **must** be ``0`` (i.e., a singleton dimension **must** be prepended to the input array ``x``). If provided an invalid axis, the function **must** raise an exception.
90+
axis: Union[int, Tuple[int, ...]]
91+
axis position(s) (zero-based). A valid axis position **must** reside on the closed-interval ``[-N-1, N]``, where ``N`` is the number of dimensions in ``x``. If an axis position is specified as a negative integer, the axis position at which to insert a singleton dimension **must** be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved axis position **must** be ``N`` (i.e., a singleton dimension **must** be appended to the input array ``x``). If provided ``-N-1``, the resolved axis position **must** be ``0`` (i.e., a singleton dimension **must** be prepended to the input array ``x``). If provided an invalid axis position, the function **must** raise an exception.
92+
93+
If ``axis`` is a tuple,
94+
95+
- each entry of ``axis`` must resolve to a unique axis position. If an entry is a negative integer, the entry **must** resolve to a positive axis position according to the rules described above.
96+
- if provided an invalid axis position, the function **must** raise an exception.
97+
- for each entry of ``axis``, the corresponding dimension in the expanded output array **must** be a singleton dimension.
98+
- for the remaining dimensions of the expanded output array, the output array dimensions **must** correspond to the dimensions of ``x`` in order.
9299
93100
Returns
94101
-------
95102
out: array
96-
an expanded output array. **Must** have the same data type as ``x``.
103+
an expanded output array. **Must** have the same data type as ``x``. If ``axis`` is an integer, the output array must have ``N + 1`` dimensions. If ``axis`` is a tuple, the output array must have ``N + len(axis)`` dimensions.
97104
98105
Raises
99106
------

0 commit comments

Comments
 (0)