I am trying to use scipy.integrate.quad in a jitted function. To do that I am overloading the function. When I call the jitted function, something is changing the given bounds of integration from floats to numba.core.types.old_scalars.Float which is not a float value. The code is below. Any help in debugging this is appreciated
from numba.extending import overload
from scipy.integrate import quad as sp_quad
from numba import njit, float64
import numpy as np
@overload(sp_quad)
def quad_overload(func, a, b):
"""
Overload for scipy.integrate.quad to allow it to be used with Numba.
"""
if not callable(func):
raise TypeError("func must be a callable function")
elif not isinstance(a, float64) or not isinstance(b, float64):
print(type(a), type(b)) # prints <class 'numba.core.types.old_scalars.Float'> <class 'numba.core.types.old_scalars.Float'>
raise TypeError("a and b must be float values")
def quad_impl(func, a, b):
return sp_quad(func, a, b)
return quad_impl(func, a, b)
@njit
def test_func(a, b):
print(a, b)
return sp_quad(np.sin, a, b)
a = 0.0
b = np.pi
print(type(a), type(b)) # prints <class 'float'>, <class 'float'>
test_func(a, b)
Do you need to overload scipy.integrate.quad?
You could call the function in object mode.
from numba import jit, objmode
from scipy.integrate import quad as sp_quad
import numpy as np
@jit
def test_func(a, b):
with objmode(result='float64'): # annotate return type
result, _ = sp_quad(np.sin, a, b)
return result
a = 0.0
b = np.pi
result = test_func(a, b)
print("Result of the integration:", result)
@zcurtisginsberg as far as I know the function scipy.integrate.quad internally calls compiled routines from the FORTRAN library QUADPACK. These routines are optimized and wrapped by SciPy for use in Python, but they are not written in Python.
Because of this, Numba cannot compile or optimize quad. Numba specializes in compiling Python and NumPy code to machine code, but it cannot recompile external libraries like QUADPACK.
Calling quad in object mode would just be a wrapper to call the function from a Numba pipeline if that would be required.