pythonperformancenumbascipy-optimizefsolve

using Numba for scipy fsolve, but get error


I want use numba for scipy.fsolve:

from scipy.optimize import fsolve
from numba import njit

@njit
def FUN12():
    XGUESS=[8.0,7.0]

    X =[0.0,0.0]
    try:
         X = fsolve(FCN3, XGUESS)
    except:
         print("error")
    return X

@njit
def FCN3(X):
    F=[0.0,0.0]
    F[0]=4.*pow(X[0],2)-3.*pow(X[1],1)-7
    F[1] = 5.*X[0]  -2. * pow(X[1] , 2)+8
    return F

FUN12()

I got this error for my code: numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend) Untyped global name 'fsolve': Cannot determine Numba type of <class 'function'>


Solution

  • Numba compatible code is only some subset of Python and Numpy functions.

    You should not run fsolve inside numba function.

    You should remove njit decorator from FUN12 function. You can keep it for FCN3.

    from scipy.optimize import fsolve
    from numba import njit
    
    # @njit  remove this line, 
    def FUN12():
        XGUESS=[8.0,7.0]
    
        X =[0.0,0.0]
        try:
             X = fsolve(FCN3, XGUESS)
        except:
             print("error")
        return X
    
    @njit
    def FCN3(X):
        F=[0.0,0.0]
        F[0]=4.*pow(X[0],2)-3.*pow(X[1],1)-7
        F[1] = 5.*X[0]  -2. * pow(X[1] , 2)+8
        return F
    
    FUN12()