Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c09526

Browse files
authored
Merge pull request #346 from steff456/broadcast-rst
PR: Transform broadcasting.md to .rst
2 parents e059ae2 + fbddbd0 commit 2c09526

File tree

2 files changed

+115
-116
lines changed

2 files changed

+115
-116
lines changed

spec/API_specification/broadcasting.md

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.. _broadcasting:
2+
3+
Broadcasting
4+
============
5+
6+
Array API specification for broadcasting semantics.
7+
8+
Overview
9+
--------
10+
11+
**Broadcasting** refers to the automatic (implicit) expansion of array dimensions to be of equal sizes without copying array data for the purpose of making arrays with different shapes have compatible shapes for element-wise operations.
12+
13+
Broadcasting facilitates user ergonomics by encouraging users to avoid unnecessary copying of array data and can **potentially** enable more memory-efficient element-wise operations through vectorization, reduced memory consumption, and cache locality.
14+
15+
Algorithm
16+
---------
17+
18+
Given an element-wise operation involving two compatible arrays, an array having a singleton dimension (i.e., a dimension whose size is one) is broadcast (i.e., virtually repeated) across an array having a corresponding non-singleton dimension.
19+
20+
If two arrays are of unequal rank, the array having a lower rank is promoted to a higher rank by (virtually) prepending singleton dimensions until the number of dimensions matches that of the array having a higher rank.
21+
22+
The results of the element-wise operation must be stored in an array having a shape determined by the following algorithm.
23+
24+
#. Let ``A`` and ``B`` both be arrays.
25+
26+
#. Let ``shape1`` be a tuple describing the shape of array ``A``.
27+
28+
#. Let ``shape2`` be a tuple describing the shape of array ``B``.
29+
30+
#. Let ``N1`` be the number of dimensions of array ``A`` (i.e., the result of ``len(shape1)``).
31+
32+
#. Let ``N2`` be the number of dimensions of array ``B`` (i.e., the result of ``len(shape2)``).
33+
34+
#. Let ``N`` be the maximum value of ``N1`` and ``N2`` (i.e., the result of ``max(N1, N2)``).
35+
36+
#. Let ``shape`` be a temporary list of length ``N`` for storing the shape of the result array.
37+
38+
#. Let ``i`` be ``N-1``.
39+
40+
#. Repeat, while ``i >= 0``
41+
42+
#. Let ``n1`` be ``N1 - N + i``.
43+
44+
#. If ``n1 >= 0``, let ``d1`` be the size of dimension ``n1`` for array ``A`` (i.e., the result of ``shape1[n1]``); else, let ``d1`` be ``1``.
45+
46+
#. Let ``n2`` be ``N2 - N + i``.
47+
48+
#. If ``n2 >= 0``, let ``d2`` be the size of dimension ``n2`` for array ``B`` (i.e., the result of ``shape2[n2]``); else, let ``d2`` be ``1``.
49+
50+
#. If ``d1 == 1``, then set the ``i``\th element of ``shape`` to ``d2``.
51+
52+
#. Else, if ``d2 == 1``, then
53+
54+
- set the ``i``\th element of ``shape`` to ``d1``.
55+
56+
#. Else, if ``d1 == d2``, then
57+
58+
- set the ``i``\th element of ``shape`` to ``d1``.
59+
60+
#. Else, throw an exception.
61+
62+
#. Set ``i`` to ``i-1``.
63+
64+
#. Let ``tuple(shape)`` be the shape of the result array.
65+
66+
Examples
67+
~~~~~~~~
68+
69+
The following examples demonstrate the application of the broadcasting algorithm for two compatible arrays.
70+
71+
::
72+
73+
A (4d array): 8 x 1 x 6 x 1
74+
B (3d array): 7 x 1 x 5
75+
---------------------------------
76+
Result (4d array): 8 x 7 x 6 x 5
77+
A (2d array): 5 x 4
78+
B (1d array): 1
79+
-------------------------
80+
Result (2d array): 5 x 4
81+
A (2d array): 5 x 4
82+
B (1d array): 4
83+
-------------------------
84+
Result (2d array): 5 x 4
85+
A (3d array): 15 x 3 x 5
86+
B (3d array): 15 x 1 x 5
87+
------------------------------
88+
Result (3d array): 15 x 3 x 5
89+
A (3d array): 15 x 3 x 5
90+
B (2d array): 3 x 5
91+
------------------------------
92+
Result (3d array): 15 x 3 x 5
93+
A (3d array): 15 x 3 x 5
94+
B (2d array): 3 x 1
95+
------------------------------
96+
Result (3d array): 15 x 3 x 5
97+
98+
99+
The following examples demonstrate array shapes which do **not** broadcast.
100+
101+
::
102+
103+
A (1d array): 3
104+
B (1d array): 4 # dimension does not match
105+
106+
A (2d array): 2 x 1
107+
B (3d array): 8 x 4 x 3 # second dimension does not match
108+
109+
A (3d array): 15 x 3 x 5
110+
B (2d array): 15 x 3 # singleton dimensions can only be prepended, not appended
111+
112+
In-place Semantics
113+
------------------
114+
115+
As implied by the broadcasting algorithm, in-place element-wise operations must not change the shape of the in-place array as a result of broadcasting.

0 commit comments

Comments
 (0)