I just wrote a simple benchmark comparing Numba and Julia, together with some discussion.
I'm wondering whether my Numba code could be fixed somehow, or if what I'm trying to do is indeed not supported by Numba.
The idea is to evaluate this function using a JIT-compiled quadrature rule.
g(p) = integrate exp(p*x) with respect to x
This is the simple quadrature function:
@nb.njit
def quad_trap(f,a,b,N):
h = (b-a)/N
integral = h * ( f(a) + f(b) ) / 2
for k in range(N):
xk = (b-a) * k/N + a
integral = integral + h*f(xk)
return integral
I can pass a JIT-compiled function to this function, like this one:
@nb.njit(nb.float64(nb.float64))
def func(x):
return math.exp(x) - 10
And that's about 10-20X faster than pure Python, which is pretty good.
Now, what I would like to do is to pass a function of x and parametrized by p, something along the lines of:
def g(p):
@nb.njit(nb.float64(nb.float64))
def integrand(x):
return math.exp(p*x) - 10
return quad_trap(integrand, -1, 1, 10000)
And doing seems to break down Numba, which becomes incredibly slow even when compared with pure Python.
Am I doing something wrong, or this feature is indeed unsupported by Numba? (I did check the documentation but I don't understand exactly where the problem is). Thanks!