'What to do if we have an array of functions?
I have an array made of function, and i want output of all the function in an array when i put input , I can do it one-by-one using a for loop as we know , but how to do it once if i want the output all at once so that it is fast.
We can regenrate the problem as follows
Suppose we have a square,cube,double and triple function as follows
def square(x):
return x**2
def cube(x):
return x**3
def double(x):
return 2*x
def triple(x):
return 3*x
Now create an array of this function, of shape 2x2, as follows
arr = np.array([[double , triple],[square , cube]]
Now I want to get the an array which gives me its value at 2 , that look something like ```
array([[2, 4],
[6, 8]])
While I can do it separately like for double
arr[0,0](2)
but i want to do all at once efficiently
Note that this function are just illustrtions, the actual problem have different array made up of different function , but they can take input of same type , that i can say
Solution 1:[1]
If your array always has the same shape then you could do it like this:
arr = np.array([i(2) for i in arr.reshape(4)]).reshape(2,2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | geanakuch |
