We can create a universal function from a Python function with the NumPy frompyfunc
function.
The following steps let us create a universal function:
Let's define a simple Python function that just doubles the input:
def double(a): return 2 * a
Create the universal function with frompyfunc
. We need to specify the number of input arguments and the number of objects returned:
import numpy def double(a): return 2 * a ufunc = numpy.frompyfunc(double, 1, 1) print "Result", ufunc(numpy.arange(4))
The code prints the following output, when executed:
Result [0 2 4 6]