Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use numba on classes?

I can found in 0.6 numba documentation some informations about how to use numba on classes:

from numba import jit, void, int_, double

# All methods must be given signatures

@jit
class Shrubbery(object):
    @void(int_, int_)
    def __init__(self, w, h):
        # All instance attributes must be defined in the initializer
        self.width = w
        self.height = h

        # Types can be explicitly specified through casts
        self.some_attr = double(1.0)

    @int_()
    def area(self):
        return self.width * self.height

    @void()
    def describe(self):
        print("This shrubbery is ", self.width,
              "by", self.height, "cubits.")

But i don't found in 0.16 documentation. Is it always possible to use numba on classes ?

like image 913
bux Avatar asked Jan 09 '23 23:01

bux


1 Answers

As of version 0.23 there is a numba.jitclass method. I can say that the following works in version 0.26

@numba.jitclass([('width', numba.float64), ('height', numba.float64)])
class Shrubbery(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h

    def area(self):
        return self.width * self.height

shrubbery = Shrubbery(10, 20)
print(shrubbery.area())

The documentation says that only methods and properties are allowed, so including the describe function will unfortunately not work at the moment.

like image 65
oschoudhury Avatar answered Feb 26 '23 00:02

oschoudhury