If I have a random function like func(x,y) = cos(x) + sen(y) + x*y how can I apply it to all the pairs of elements in 2 arrays?
I found https://docs.scipy.org/doc/nu
One way to solve this is broadcasting:
import numpy as np
def func(x, y):
x, y = np.asarray(x)[:, None], np.asarray(y)
return np.cos(x) + np.sin(y) + x*y
The [:, None]
adds another dimension to an array and therefor triggers NumPys broadcasting.
>>> func([1,2], [3,4])
array([[ 3.68142231, 3.78349981],
[ 5.72497317, 6.82705067]])