Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/array_api_stubs/_2021_12/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def concat(
"""


def expand_dims(x: array, /, *, axis: int = 0) -> array:
def expand_dims(x: array, /, axis: int) -> array:
"""
Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by ``axis``.

Expand Down
2 changes: 1 addition & 1 deletion src/array_api_stubs/_2022_12/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def concat(
"""


def expand_dims(x: array, /, *, axis: int = 0) -> array:
def expand_dims(x: array, /, axis: int) -> array:
"""
Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by ``axis``.

Expand Down
2 changes: 1 addition & 1 deletion src/array_api_stubs/_2023_12/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def concat(
"""


def expand_dims(x: array, /, *, axis: int = 0) -> array:
def expand_dims(x: array, /, axis: int) -> array:
"""
Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by ``axis``.

Expand Down
2 changes: 1 addition & 1 deletion src/array_api_stubs/_2024_12/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def concat(
"""


def expand_dims(x: array, /, *, axis: int = 0) -> array:
def expand_dims(x: array, /, axis: int) -> array:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by ``axis``.

Expand Down
17 changes: 12 additions & 5 deletions src/array_api_stubs/_draft/manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,28 @@ def concat(
"""


def expand_dims(x: array, /, *, axis: int = 0) -> array:
def expand_dims(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array:
"""
Expands the shape of an array by inserting a new axis of size one at the position specified by ``axis``.
Expands the shape of an array by inserting a new axis of size one at the position (or positions) specified by ``axis``.

Parameters
----------
x: array
input array.
axis: int
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. Default: ``0``.
axis: Union[int, Tuple[int, ...]]
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.
Comment thread
kgryte marked this conversation as resolved.
Outdated

If ``axis`` is a tuple,

- 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.
- if provided an invalid axis position, the function **must** raise an exception.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy raises AxisError, which derives from IndexError (which torch.unsqueeze raises) and ValueError (which jax.numpy raises). So short of adding AxisError with a prescribed inheritance hierarchy we cannot be more specific on what exception to raise.

- for each entry of ``axis``, the corresponding dimension in the expanded output array **must** be a singleton dimension.
- for the remaining dimensions of the expanded output array, the output array dimensions **must** correspond to the dimensions of ``x`` in order.

Returns
-------
out: array
an expanded output array. **Must** have the same data type as ``x``.
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.

Raises
------
Expand Down