Universal Functions

With NumbaPro, universal functions (ufuncs) can be created by applying the vectorize decorator on to simple scalar functions. A ufunc can operates on scalars or NumPy arrays. When used on arrays, the ufunc apply the core scalar function to every group of elements from each arguments in an element-wise fashion. NumPy Broadcasting is applied to every argument with mismatching dimensions.

Example: Basic

Here is a simple example to perform element-wise addition:

import numpy
from numbapro import vectorize

# Create a ufunc
@vectorize(['float32(float32, float32)',
            'float64(float64, float64)'])
def sum(a, b):
    return a + b

# Use the ufunc
a = numpy.arange(10)
b = numpy.arange(10)
result = sum(a, b)      # call the ufunc

print("a = %s" % a)
print("b = %s" % b)
print("sum = %s" % result)

The ufunc is compiled to operate on float32 and float64 arrays. It is used to compute element-wise addition of array a and b which are arrays of numpy.float64 with 10 elements. The output

a = [0 1 2 3 4 5 6 7 8 9]
b = [0 1 2 3 4 5 6 7 8 9]
sum = [  0.   2.   4.   6.   8.  10.  12.  14.  16.  18.]


A generalization of the usage of the vectorize decorator is described in this section.

vectorize(type_signatures[, target='cpu'])

Returns a vectorizer object to be applied to python functions.

  • type_signatures – an iterable of type signatures, which are either function type object or a string describing the function type.
  • target – a string for hardware target; e.g. “cpu”, “parallel”, “gpu”.

a vectorizers object.

To use multithreaded version, change the target to “parallel”:

from numbapro import vectorize

@vectorize(['float32(float32, float32)'], target='parallel')
def sum(a, b):
    return a + b

For CUDA target, use “gpu” for target: <div class=”highlight-default”><div class=”highlight”><pre><span></span><span class=”kn”>from</span> <span class=”nn”>numbapro</span> <span class=”k”>import</span> <span class=”n”>vectorize</span>

@vectorize(['float32(float32, float32)'], target='gpu') def sum(a, b): return a + b