I am looking for a way to apply a function to all elements of a numpy array. The function receives not only the value but also the indices of the element as arguments. The goal is perfomance on large 2- or 3-dim. arrays.
(I know there are several ways to do that with a function that receives the value of an element only)
The code to be replaced is
def foo(x, i, j)
return (i*x)**2 - (j*x)**3 # or some other fancy stuff
...
arr = np.zeros((nx, ny))
...
# nested loop to be replaced, e.g. via vectorization
for i in range(nx):
for j in range(ny):
arr[i,j] = foo(arr[i,j], i, j)