Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions spec/draft/API_specification/manipulation_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Objects in API
:template: method.rst

broadcast_arrays
broadcast_shapes
broadcast_to
concat
expand_dims
Expand Down
30 changes: 30 additions & 0 deletions src/array_api_stubs/_draft/manipulation_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = [
"broadcast_arrays",
"broadcast_shapes",
"broadcast_to",
"concat",
"expand_dims",
Expand Down Expand Up @@ -35,6 +36,35 @@ def broadcast_arrays(*arrays: array) -> List[array]:
"""


def broadcast_shapes(*shapes: Tuple[int, ...]) -> Tuple[int, ...]:
"""
Broadcasts one or more shapes against one another.

Parameters
----------
shapes: Tuple[int, ...]
an arbitrary number of to-be broadcasted shapes.

Returns
-------
out: Tuple[int, ...]
a broadcasted shape.
Copy link
Member

Choose a reason for hiding this comment

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

This should probably say something like "The shape should match the regular broadcasting rules as documented in :ref:broadcasting" to make the specification a little bit more tight.


Raises
------
ValueError
If provided shapes which are not broadcast compatible (see :ref:`broadcasting`), a ``ValueError`` **should** be raised.

Notes
-----

- If not provided one or more arguments, the function **must** return an empty tuple.

.. note::
Array libraries which build computation graphs (e.g., ndonnx and Dask) commonly support shapes having dimensions of unknown size. If a shape contains a value other than an integer (e.g., ``None`` for a dimension of unknown size), behavior is unspecified and thus implementation-defined. Array-conforming libraries **may** choose to propagate such values (e.g., if a shape contains a dimension size of ``None``, the returned broadcasted shape also has a corresponding dimension having a size equal to ``None``) or raise an exception.
Copy link
Member

Choose a reason for hiding this comment

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

This can be phrased such that it works for computation graphs in the expected manner I believe, since the broadcasting math is abstract anyway and any sentinels can propagate correctly. To be discussed in the next call I think.

"""


def broadcast_to(x: array, /, shape: Tuple[int, ...]) -> array:
"""
Broadcasts an array to a specified shape.
Expand Down