For example, I can't understand the output of the following function:
@nb.guvectorize(["void(float64, float64, float64, float64)"], "(),()->(),()")
def add_subtract(a, b, res1, res2):
res1 = a + b
res2 = a - b
When I call it,
add_subtract(np.array([3.0,5.0]), np.array([6.0,7.0]))
oddly enough, it will show:
(array([6.95307160e-310, 1.23643049e-311]), array([0., 0.]))
It seems that this function is trying to return 0. How come the function doesn't return [[9,-3],[12,-2]]?
Output arrays are passed uninitialized to the function. If you assign a new value to res1
and res2
locals, you are replacing the arrays by new ones. This won't change the arrays passed to the function, which remain uninitialized.
Instead, you need to replace their contents. The following example works:
@nb.guvectorize("void(float64[:], float64[:], float64[:], float64[:])",
"(),()->(),()")
def add_subtract(a, b, res1, res2):
res1[:] = a + b
res2[:] = a - b