assuming I have any function such as
f(x, y, z) = xyz
what's the fastest way of calculating every value for f given three linear input arrays x, y, and z?
Of course I can do something along the lines of,
import numpy as np
x = np.linspace(-1., 1., 11)
y = np.linspace(-1., 1., 11)
z = np.linspace(-1., 1., 11)
for xi, xx in enumerate(x):
for yi, yy in enumerate(y):
for zi, zz in enumerate(z):
f(xi, yi, zz) = xx*yy*zz
but this is probably not the best way to do it, especially if the input arrays become larger. Is there a better way of doing it, such as with a list comprehension (but multidimensional) or any other way? Help is appreciated.
x * y * z?x * y * zwill give me a resulting array of shape (11, ) instead of every combination of x, y, z, which will be an array of shape (11, 11, 11) for the above case.f(xi, yi, zz) = xx*yy*zzdoesn't work, does it? Python doesn't allow you to assign to a function call!x[:,None,None] * y[None,:,None]*z[None,None,:]? This usesbroadcasting- it's a key concept when working with numpy arrays.