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]:= |
Out[1]= |
Here this alignment is not supported:
In[2]:= |
Out[2]= |
Use Broadcasting to achieve the same effect as in NumPy:
In[3]:= |
Out[3]= |
Plus does not thread over arrays of different dimensions:
In[4]:= |
Out[4]= |
Define the Broadcasting Plus operator:
In[5]:= |
Now this kind of operation can be done:
In[6]:= |
Out[6]= |
Listable functions in the Wolfram Language can effectively thread when initial dimensions match:
In[7]:= |
Out[7]= |
NumPy does not support this initial alignment:
In[8]:= |
Out[8]= |
If dimensions are not broadcastable, then Broadcasting will report an error and will not return any results:
In[9]:= |
This work is licensed under a Creative Commons Attribution 4.0 International License