I'm trying to create a simple cython module and have the following problem. I would like to create a function like:
cdef float calc(float[:] a1, float[:] a2):
cdef float res = 0
cdef int l = len(a2)
cdef float item_a2
cdef float item_a1
for idx in range(l):
if a2[idx] > 0:
item_a2 = a2[idx]
item_a1 = a1[idx]
res += item_a2 * item_a1
return res
When the function is being executed, a1 and a2 params are python lists. Therefore I get the error:
TypeError: a bytes-like object is required, not 'list'
I just need to make such calculations and nothing more. But how shall I define input params float[:] a1
and float[:] a2
if I need to maximize speed up using C?
Probably it's necessary to convert lists to arrays manually?
P.S. would appreciate also if you can also explain to me whether it's necessary to declare cdef float item_a2
explicitly to perform multiplication (in terms of performance) or it is equally to result += a2[idx] * a1[idx]