Skip to content

Commit 9db13b3

Browse files
authored
feat: add broadcast_shapes to the specification
PR-URL: data-apis#983 Closes: data-apis#893 Reviewed-by: Ralf Gommers <ralf.gommers@gmail.com> Reviewed-by: Lucas Colley Reviewed-by: Evgeni Burovski
1 parent 41f9880 commit 9db13b3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spec/draft/API_specification/manipulation_functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Objects in API
1919
:template: method.rst
2020

2121
broadcast_arrays
22+
broadcast_shapes
2223
broadcast_to
2324
concat
2425
expand_dims

src/array_api_stubs/_draft/manipulation_functions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__all__ = [
22
"broadcast_arrays",
3+
"broadcast_shapes",
34
"broadcast_to",
45
"concat",
56
"expand_dims",
@@ -35,6 +36,33 @@ def broadcast_arrays(*arrays: array) -> Tuple[array, ...]:
3536
"""
3637

3738

39+
def broadcast_shapes(*shapes: Tuple[Optional[int], ...]) -> Tuple[Optional[int], ...]:
40+
"""
41+
Broadcasts one or more shapes against one another.
42+
43+
Parameters
44+
----------
45+
shapes: Tuple[Optional[int], ...]
46+
an arbitrary number of to-be broadcasted shapes.
47+
48+
Returns
49+
-------
50+
out: Tuple[Optional[int], ...]
51+
a single broadcasted shape obtained by applying broadcasting rules (see :ref:`broadcasting`) to each of the input shapes against one another.
52+
53+
Raises
54+
------
55+
ValueError
56+
If provided shapes which are not broadcast compatible (see :ref:`broadcasting`), a ``ValueError`` **should** be raised.
57+
58+
Notes
59+
-----
60+
61+
- If not provided one or more arguments, the function **must** return an empty tuple.
62+
- Array-conforming libraries which build computation graphs (e.g., ndonnx and Dask) and which need to provide a mechanism for indicating dimensions of unknown size **may** accept non-integer values (e.g., ``None``). For these libraries, dimensions of unknown size **must** be propagated to the returned shape.
63+
"""
64+
65+
3866
def broadcast_to(x: array, /, shape: Tuple[int, ...]) -> array:
3967
"""
4068
Broadcasts an array to a specified shape.

0 commit comments

Comments
 (0)