Vectorize
Overview
Time: 10 min
Learn how to use vectorize to implement ufunc.
Understand how to specify multiple signatures for vectorized functions.
Numba’s vectorize feature enables Python functions that take scalar inputs to be used as NumPy ufuncs.
By applying the vectorize() decorator, Numba compiles a pure Python function into a ufunc that can
process NumPy arrays with performance comparable to traditional ufuncs written in C. As with any
Numba function, you can choose between eager or lazy mode for vectorization.
1from numba import vectorize, int32, int64, float32, float64
2
3@vectorize([int32(int32, int32),
4 int64(int64, int64),
5 float32(float32, float32),
6 float64(float64, float64)])
7vec_test(x, y):
8 return x + y
When specifying multiple signatures, make sure to list the most specific ones before the more general ones.
Key Points
Numba’s vectorize feature allows Python functions to be used as NumPy ufuncs.
The vectorize() decorator compiles a Python function into a ufunc.
You can specify multiple signatures for vectorized functions, with more specific ones listed first.