Search code examples
pythonnumba

Numba can not compile a simple function


I am trying to compile a simple function that takes an python list and then split it according to a list of index. I am new to numba and the docs have not help me.

Problem I cant successfully compile the function

Code to compile The arr variable is a python list of floats or integers and idx is a numpy array or python list of integers, the result out should be a 2d python list of floats or integers according to arr input

from numba.pycc import CC

cc = CC('trial')
# Uncomment the following line to print out the compilation steps
cc.verbose = True



@cc.export('split_numba', 'f8[:](f8[:], i4[:])')
def split_numba(arr, idx):
    out = []
    s = 0
    for n in range(len(idx)):
        e = idx[n]
        out.append(arr[s:e])
        s = e

    return out

if __name__ == "__main__":
    cc.compile()

Error

No conversion from list(array(float64, 1d, A))<iv=None> to array(float64, 1d, A) for '$60return_value.1', defined at None

File "RUT_COMPILE.py", line 38:
def split_numba(arr, idx):
    <source elided>
        
    return out
    ^

During: typing of assignment at C:\Users\chelo\OneDrive\ALCANTARILLADO_PyQt5\00_MODULOS\RUT_COMPILE.py (38)

File "RUT_COMPILE.py", line 38:
def split_numba(arr, idx):
    <source elided>
        
    return out

Solution

  • The output type of the function is not correct since out is a list of arrays. It should be 'List(f8[:])(f8[:], i4[:])' instead of 'f8[:](f8[:], i4[:])'.

    Note that the specified input is not a Python list: f8[:] reference a Numpy array type with 64-bit float items. The List type provided by Numba is also not a pure-Python list (called "reflected list" in the Numba documentation). It is an alternative list type that is typed (so Numba needs to convert pure-Python lists to typed lists for input lists and the reverse operation for output lists). Reflected lists support heterogeneous typed items. Such operation introduce an overhead which can be big if the amount of computation is small (this is your case).