numba.jit()
allows entering a type signature but I can't figure out what the signature for a zero dimensional array is.
For example:
numba.jit('void(float32, float32[:])')
says the function return is void and the input arguments are float32 scalar and float32 1-D array.
But what is instead of a scalar I want to pass in a 0-dimensional array. What's the type signature? I tried the obvious float32[]
but that didn't seem to work.
In case you are wondering how one gets a 0-D array in numpy you do it like this:
a = numpy.array(2)
which is different than
a = numpy.array([2])
the latter is a 1-D array.
This is how you can do it using numba.types.Array
:
import numba as nb
import numpy as np
# |---------0d int array---------|
@nb.njit(nb.types.Array(nb.int64, 0, "C")())
def func():
return np.array(2)
Here I used that the returned value will be a C-contiguous int64
array with 0
dimensions. Adjust these as needed.
In my experience there is rarely a use-case (see "Benefit and Limitations of Ahead-of-Time compilation") for explicitly typed functions in numba - except for compilation times or in case one needs to avoid numba using already inferred types when it should compile a new function. So, personally, I wouldn't use these signatures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With