If I have an 1D numpy.ndarray b and a Python function f that I want to vectorize, this is very easy using the numpy.vectorize function:
c = numpy.vectorize(f)(a).
But if f returns a 1D numpy.ndarray instead of a scalar, how can I build a 2D numpy.ndarray instead? (That is, I want every 1D numpy.ndarray returned from f to become a row in the new 2D numpy.ndarray.)
Example:
def f(x):
return x * x
a = numpy.array([1,2,3])
c = numpy.vectorize(f)(a)
def f_1d(x):
return numpy.array([x, x])
a = numpy.ndarray([1,2,3])
d = ???(f_1d)(a)
In the above example c would become array([1, 4, 9]). What should ??? be replaced with if d should become array([[1, 1], [2, 2], [3, 3]])?
d=np.kron(np.ones((1,2),dtype=np.int), a.reshape((-1,1))