I want to multiply two numpy arrays of different shapes along a specific axes without swapping them manually or adding “dummy” dimensions if possible.
For two arrays, let’s say A of shape (3,2,2) and B of shape (3) i want to get the prodcut of the form
np.swapaxes(np.swapaxes(A,0,2)*B),0,2)
or alternatively
A*B[:,None,None]
However for larger arrays both methods seem to be adding noticeable calculation time to the process compared with initializing A with a shape of (2,2,3) to begin with. I can not do that though because i need A to be of the (3,2,2) shape for a later multiplication operation.
Can I avoid the double swapping of axes or the adding of “None” dimensions to improve calculation time?