The problem is that the numba version of np.min
requires an array
as input.
from numba import njit
import numpy as np
@njit
def test_numba_version_of_numpy_min(inp):
return np.min(inp)
>>> test_numba_version_of_numpy_min(np.array([1, 2])) # works
1
>>> test_numba_version_of_numpy_min([1, 2]) # doesn't work
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function amin at 0x000001B5DBDEE598>) with argument(s) of type(s): (reflected list(int64))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
The better solution would be to just use the numba version of Pythons min
:
from numba import njit
import numpy as np
@njit
def availarray(length):
out = np.ones(14)
if length > 0:
out[0:min(length, 14)] = 0
return out
Since both np.min
and min
are actually Numba versions of these functions (at least in njit
ted functions) min
should also be much faster in this case. However it's unlikely to be noticeable because the allocation of the array and setting some of the elements to zero will be the dominant runtime contributors here.
Note that you don't even need the min
call here - because slicing implicitly stops at the end of the array even if a bigger stop index is used:
from numba import njit
import numpy as np
@njit
def availarray(length):
out = np.ones(14)
if length > 0:
out[0:length] = 0
return out