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
28 changes: 28 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,33 @@ 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 single broadcasted shape obtained by applying broadcasting rules (see :ref:`broadcasting`) to each of the input shapes against one another.

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.
- 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), array-conforming libraries **must** propagate such values (e.g., if a shape contains a dimension size of ``None``, the returned broadcasted shape **must** also have a corresponding dimension having a size equal to ``None``).
Copy link
Member

Choose a reason for hiding this comment

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

I've to admit I'm not exactly sure what this clause means for a library that does not support a sentinel value. E.g., what must numpy do for inputs of (1, 2, None) and (1, 2, 1)?

  • does it raise a ValueError since the broadcasting section does not mention None (hence shapes are not broadcast-compatible), per the Raises section?
  • or does it propagate the None, per this clause?

Maybe a way out is to phrase it, roughly, "An array library may accept a value other than integer (e.g. None) for one or multiple dimensions. This is common for libraries that build computational graphs, and is used to indicate a dimension of unknown size. Array-conforming libraries which accept non-integer values for unknown dimensions must propagate such values...."

Copy link
Member

Choose a reason for hiding this comment

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

numpy will raise ValueError. Your proposed phrasing looks good to me.

"""


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