I have a numpy array with functions and another one with values:
f = np.array([np.sin,np.cos,lambda x: x**2])
x = np.array([0,0,3])
I want to apply each function to each element in x. This can be easily done as
np.array([F(X) for F,X in zip(f,x)])
Is there a more efficient way that does not involve a for loop? Something like just f(x) (which of course does not works).
forloop, or do you want to avoid executing aforloop (say, via vectorisation)?forloop and replace it for some more efficient vectorized way.listandmathfunctions instead ofnumpywill be faster. List iterate faster, andmath.sin(1)is faster thannp.sin(1).farray isobjectdtype. The fast, no-loop, compiled methods work with numeric dtypes.objectdtype is effectively a list - the elements are references to objects. The vauntednumpy"vectorization" means applying compiled methods to the arrays, moving the loops to compiled code.