TypingError: Failed in nopython mode pipeline (step: nopython frontend) Untyped global name 'nhat_lam': Cannot type list element type <class 'function'>

In a program (a simulation using sympy.physics.mechanics), I use this function:

@njit
def ellipse_street(y, args, kk, zaehler1):
    TEST = List.empty_list(types.float64)
    x0  = (args[12 + kk], args[12 + 2*n + kk])                    
    for epsilon in np.linspace(1.e-15, 2.*np.pi, int(zaehler1/min_winkel)):
        args[12 + n + kk] = epsilon
        if nhat_lam[kk](*[y[i] for i in range(n)], *[args[12 + n + jj] for jj in range(n)], a1, b1)[1] <= 0.:
            args1 = [y[i] for i in range(3*n)] + [args[12 + jj] for jj in range(n) if jj != kk] + [args[12 + 2*n +jj] for jj in range(n) if jj != kk] + [args[12 + n + jj] for jj in range(n)] + [a1, b1, amplitude1, frequenz1] + [kk]
            ergebnis = root(func_x1_l1_1, x0, args1) #, method='broyden1')
            args[12 + kk]       = ergebnis.x[0]
            args[12 + 2*n + kk] = ergebnis.x[1]
                
            if parallel_street_lam[kk](*[y[jj] for jj in range(n)], *[args[12 + jj] for jj in range(3*n)], a1, b1, amplitude1, frequenz1) < min_winkel:
                TEST.append((*ergebnis.x, epsilon))
    
    return TEST

But I get this error message:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name ‘nhat_lam’: Cannot type list element type <class ‘function’>

nhat_lam is a list, where each element is a numpy function created with sympy.lambdify(…)

I am new to numba, I do not understand this message at all.
Any help is greatly appreciated!

Hi Peter,

One thing ahead: Numba cannot translate arbitrary functions, but a quick google suggests that the results of sympy.lambdify might work, so maybe you are lucky.

The error states Untyped global name ‘nhat_lam’: Cannot type list element type <class ‘function’>
That pretty much tells you the issue here: Numba does not know how to treat this list, as it does not have sufficient information about the type of element in that list. This is certainly related to the fact that it is not an argument to your function but picked up from a generic Python object in the scope outside of your ellipse_street function.
Here you are dealing with a list of functions (i.e. you need a FunctionType, which might make this a little finicky). That said, I guess this could work, but you need to know the exact signature of the function so can create the correct type and inform numba about it (and this type has to be uniform throughout the list).

One way to attack this, could be to use a numba.typed.List for nhat_lam instead of a python list, maybe that will suffice.

I also advice to break this down into a minimal reproducer. Find a minimal failing case, that also includes the definition of nhat_lam (again simplify where you can) - no need for physical results, just code that sketches the general structure and steps of what you are trying to achieve in the end.
As this is a compilation issue reducing everything to bare bones will make it easier for you and others to pick apart and diagnose :slight_smile:

Good luck!

Dear Hannes,
Thanks for your prompt reply!

I could fix the nhat_lam issue by writing nhat_lam = njit(sympy.lambdify(…)) for each of the functions in the list nhat_lam. No idea, why this worked.
But then another long list of other errors came.

I believe, my program is just too convoluted, as you rightly pointed out. For example I use sympy.Piecewise((…), (…)…), the only way I know how to approach an if…elif…else statement in sympy.
I think, numba objected to this, too, and possibly a myrad of other things.

I think, I am reasonably o.k. at simulations with sympy.physics.mechanics, but complete novice in numba.
I will take your advice, and try simpler simulations to learn more about numba.

Thanks again!!