Suppose that I have n
functions f1,...,fn
where each function takes two numpy ndarrays X,Y
of the same size and returns an ndarray Z
of the same size. Is there a way to create the ndarray [f1(X,Y),...,fn(X,Y)]
without using for loops or list comprehension?
One can do
import numpy as np
F = [f1,f2,...,fn] # list of n functions
X = np.zeros(M,N)
Y = np.zeros(X.shape)
Z = np.concatenate([f(X,Y) for f in F])
but then I used the loop [f(X,Y) for f in F]
. I also though about creating the following function:
def F(i,X,Y):
"""
Returns fi(X,Y)
"""
return ...
and then use somthing like np.fromfunction()
but I can’t figure out how.
UserA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.