I use np.roll () to perform averaging in the neighborhood of neighboring ones, but I have a feeling that there are faster ways. Here is a simplified example , but imagine 3 dimensions and more complex averaging of “stencils”. For example, see Section 6 of this paper .
Here are a few lines from a simplified example:
for j in range(nper):
phi2 = 0.25*(np.roll(phi, 1, axis=0) +
np.roll(phi, -1, axis=0) +
np.roll(phi, 1, axis=1) +
np.roll(phi, -1, axis=1) )
phi[do_me] = phi2[do_me]
So should I look for something that returns views instead of arrays (it seems to return arrays of returns)? In this case, each time it causes the initialization of a new array? I noticed that the overhead is huge for small arrays.
In fact, it is most efficient for arrays from [100,100] to [300,300] on my laptop. Perhaps the cache problems are above this.
Will it scipy.ndimage.interpolation.shift()
work better, as implemented here , and if so, is it fixed? In the above example, I still discard the wrapped parts, but not always.
note: in this question. I am only looking for what is available in NumPy / SciPy. Of course, there are many good ways to speed up Python and even NumPy, but this is not what I am looking for here because I am really trying to understand NumPy better. Thanks!