I have 2 array shapes which are broadcastable against eachother.
e.g. (2, 2, 1) and (2, 3)
I want a function that takes those shapes and gives me an iterator returning the indices from these arrays with these shapes that would be broadcast together, and the indices in the resulting output array.
iter, output_shape = broadcast_indeces_iterator((2, 2, 1), (2, 3))
assert output_shape == (2, 2, 3)
for in1_ix, in_2_ix, out_ix in iter:
print (in1_ix, in_2_ix, out_ix)
results in output:
(0, 0, 0), (0, 0), (0, 0, 0)
(0, 0, 0), (0, 1), (0, 0, 1)
(0, 0, 0), (0, 2), (0, 0, 2)
(0, 1, 0), (1, 0), (0, 1, 0)
(0, 1, 0), (1, 1), (0, 1, 1)
(0, 1, 0), (1, 2), (0, 1, 2)
(1, 0, 0), (0, 0), (1, 0, 0)
(1, 0, 0), (0, 1), (1, 0, 1)
(1, 0, 0), (0, 2), (1, 0, 2)
(1, 1, 0), (1, 0), (1, 1, 0)
(1, 1, 0), (1, 1), (1, 1, 1)
(1, 1, 0), (1, 2), (1, 1, 2)
np.broadcast does something close but wants actual created arrays.
- Note to NumPy people: It would be nice for np.broadcast to have an additional argument allowing you to not iterate, for instance, over the last 2 dimensions. This would also solve my problem.