How to use numba for a member function of a class?

I am using the stable version of Numba 0.30.1.

I can do it:

import numba as nb @nb.jit("void(f8[:])",nopython=True) def complicated(x): for a in x: b = a**2.+a**3. 

as a test case, and the acceleration is huge. But I don’t know how to proceed if I need to speed up a function inside a class.

 import numba as nb def myClass(object): def __init__(self): self.k = 1 #@nb.jit(???,nopython=True) def complicated(self,x): for a in x: b = a**2.+a**3.+self.k 

What type of numba do I use for the self object? I need to have this function inside the class, since it needs to access the member variable.

+5
source share
1 answer

You have several options:

Use jitclass ( http://numba.pydata.org/numba-doc/0.30.1/user/jitclass.html ) to "numba -ize" it all.

Or make the member function a wrapper and pass the member variables via:

 import numba as nb @nb.jit def _complicated(x, k): for a in x: b = a**2.+a**3.+k def myClass(object): def __init__(self): self.k = 1 def complicated(self,x): _complicated(x, self.k) 
+3
source

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


All Articles