I have a function f which I am using to evolve a Numpy array z repeatedly. Thus my code looks like this:
for t in range(100):
z = f(z)
However, now I want to combine elements of array while evolving. For example, in simple Python, I would like to do something like this:
N = len(z)
for t in range(100):
for i in range(len(z)):
z_new[i] = f(z[i]) + z[(i-1)%N] + z[(i+1)%N]
z = z_new
How can achieve the same thing in Numpy vector operations so that I wouldn't have to compromise with the great speed that Numpy gives me?
Thanks in advance
iis the index of the last element, what willz[i+1]be?z[(i-1)%N]andz[(i+1)%N]so thatz[i+1] = z[0]wheniis last element. Also, hereN = len(z).z[1:]-z[:-1]. If you have to wrap around you will have to use slower advanced indexing or concatenation. Look at the internals ofroll. Look also attakeandput.