To broadcast operations (such as addition) between arrays in NumPy, trailing dimensions have to be equal (or be of length 1).
In the example given above the 3D array and 2D array have shape (lengths of dimensions):
(2, 3, 4)
(2, 3)
That is - the suffixes do not agree (4 != 3 and 3 != 2) and NumPy raises an error.However, for the same operation in J the prefixes agree:
(2, 3, 4)
(2, 3)
and the addition gives the expected result.To add the arrays with these shapes in NumPy, one method is transpose each array (reverse order of the dimensions), add these arrays, and then transpose back:
(a.T + b.T).T