I am trying to vectorize a function which has 2 inputs, and outputs a np.array, of shape =(4,). The function looks like this:
def f(a, b):
return np.array([a+b, a-b, a, b])
I was able to vectorize the function, using the signature parameter, however it only works if I exclude one of the parameters using the excluded argument of np.vectorize:
This works:
vec = np.vectorize(f, signature='()->(n)', excluded=[1])
x = np.arange(5)
y = 3
vec(x, y)
>> output:
array([[ 3, -3, 0, 3],
[ 4, -2, 1, 3],
[ 5, -1, 2, 3],
[ 6, 0, 3, 3],
[ 7, 1, 4, 3]])
However if I take out the excluded argument, things don't go as planned.
This does not work:
vec = np.vectorize(f, signature='()->(n)')
x = np.arange(5)
y = 3
vec(x, y)
>> Error:
TypeError: wrong number of positional arguments: expected 1, got 2
How can I make the vectorized function able to receive an array/list of input values for either one (or both) of the input parameters?
The expected output would be a vec function which would enable calling it with multiple inputs for either one of the input parameters.