Ideally, you'd do this by changing the implementation of f to use techniques that handle high-dimensional input appropriately. For example, you might change np.sum(whatever) to np.sum(whatever, axis=-1) to get a sum over the last axis instead of the whole array. This would produce the most efficient results, but it might be difficult or impossible, depending on f.
The slower, much easier answer is np.apply_along_axis:
A = np.apply_along_axis(f, -1, R)
This is prettier than 3 for loops, but it probably won't be any more efficient.
fis. Hopefully, it'll be as simple as addingaxisarguments in a few places.fis so complicated that I don't even want to post it here.Rto 2d, so you index on just one axis, and slice the other. Then reshape the result back. In other words, temporarily 'flatten' thei,j,kdimensions. It won't save iteration steps, but should simplify the code.