Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Calculate multidimensional arrays according to the broadcasting rules of NumPy
| ResourceFunction["Broadcasting"][expression] calculates a multidimensional array from expression using the broadcasting rules. | |
| ResourceFunction["Broadcasting"][op] converts an op to a ResourceFunction["Broadcasting"] operator. | 
In NumPy, multidimensional array operations are tail-aligned:
| In[1]:= | ![ExternalEvaluate["Python", "from numpy import zeros,ones
(zeros((8,5,7,4))+ones((7,4))).shape"]](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/5eb3d502284545d0.png) | 
| Out[1]= |  | 
Here this alignment is not supported:
| In[2]:= | ![Array[0 &, {8, 5, 7, 4}] + Array[1 &, {7, 4}] // Dimensions](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/0b8b255d7cfdb5d1.png) | 
| Out[2]= |  | 
Use Broadcasting to achieve the same effect as in NumPy:
| In[3]:= | ![ResourceFunction["Broadcasting"][
  Array[0 &, {8, 5, 7, 4}] + Array[1 &, {7, 4}]] // Dimensions](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/68b2a0e875bceb49.png) | 
| Out[3]= |  | 
Plus does not thread over arrays of different dimensions:
| In[4]:= | ![Plus[{{x, y}, {z, w}}, {{a}, {b}}, c]](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/60656e28127a9ef6.png) | 
| Out[4]= |  | 
Define the Broadcasting Plus operator:
| In[5]:= | ![bPlus = ResourceFunction["Broadcasting"][Plus];](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/1d663aa12b665864.png) | 
Now this kind of operation can be done:
| In[6]:= | ![bPlus[{{x, y}, {z, w}}, {{a}, {b}}, c]](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/24d4da1d6982671f.png) | 
| Out[6]= |  | 
Listable functions in the Wolfram Language can effectively thread when initial dimensions match:
| In[7]:= | ![Array[0 &, {4, 7, 5, 8}] + Array[1 &, {4, 7}] // Dimensions](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/135952072d7a7f2d.png) | 
| Out[7]= |  | 
NumPy does not support this initial alignment:
| In[8]:= | ![ExternalEvaluate["Python", "from numpy import zeros,ones
(zeros((4,7,5,8))+ones((4,7))).shape"]](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/2d93a5b9985622af.png) | 
| Out[8]= |  | 
If dimensions are not broadcastable, then Broadcasting will report an error and will not return any results:
| In[9]:= | ![ResourceFunction["Broadcasting"][Plus][Array[a, {3}], Array[b, {4, 2}]]](https://www.wolframcloud.com/obj/resourcesystem/images/2f6/2f65b091-b849-4cc8-a935-18287b6968db/15cb13cc27d36624.png) | 
This work is licensed under a Creative Commons Attribution 4.0 International License