Function Types in numba

Currently the best way to work with higher order functions in numba?

I implemented the secant method :

def secant_method_curried (f):
    def inner (x_minus1, x_0, consecutive_tolerance):
        x_new = x_0
        x_old = x_minus1
        x_oldest = None
        while abs(x_new - x_old) > consecutive_tolerance:
            x_oldest = x_old
            x_old = x_new
            x_new = x_old - f(x_old)*((x_old-x_oldest)/(f(x_old)-f(x_oldest)))
        return x_new
    return numba.jit(nopython=False)(inner)

The problem is that there is no way to tell numba what to feat doube(double), so the code above breaks into nopython=True:

TypingError: Failed at nopython (nopython frontend)
Untyped global name 'f'

It looks like there was a FunctionType in previous versions, but was deleted / renamed: http://numba.pydata.org/numba-doc/0.8/types.html#functions

On this page, they mention something like numba.addressof (), which seems useful but dates back to 4 years.

+3
source share
1 answer

. , , jit secant_method_curried:

>>> from numba import njit

>>> def func(x):  # an example function
...     return x

>>> p = secant_method_curried(njit(func))  # jitted the function

>>> p(1,2,3)
2.0

njit(func) jit(func).


numba , , :

[...] JIT- , jitted-.

+3

Source: https://habr.com/ru/post/1667814/


All Articles