Dear list,
I have a (nopython) function for which it would be nice if users can either provide an array or a scalar. However, i can't find a way to check whats provided from within the Numba code. And its necessary to now because an array would need to be indexed as opposed to a scalar. Python functions like isinstance, type, hasattr etc dont work. So is there a way to make this distinction within a Numba function? A workaround would be to always cast to an array on forehand, with something like np.full_like(), perhaps that's even faster then checking it in each iteration of the loop like in the example below.
A sample functions:
@numba.autojit(nopython=True)
def some_func(a, b):
# a is always an array
# b could be an array or scalar
c = 0
for i in range(len(a)):
###
# check if b should be indexed or not
# something like
if isinstance(b, np.ndarray):
b_val = b[i]
else:
b_val = b
####
c += a[i] + b_val
return c
a = np.random.randn(10)
#b = 5
b = np.random.randn(10)
some_func(a, b)
Is there a way in Numba to check if a variable is a scalar or an array?
Regards,
Rutger