I'm trying to make a class that could be a part of jitclass
but has some attribute that are them-self jitclass
objects.
For example, if I have two class with the decorator @jitclass
, I would like instanced those in a third class (combined
).
import numpy as np
from numba import jitclass
from numba import boolean, int32, float64,uint8
spec = [
('type' ,int32),
('val' ,float64[:]),
('result',float64)]
@jitclass(spec)
class First:
def __init__(self):
self.type = 1
self.val = np.ones(100)
self.result = 0.
def sum(self):
self.result = np.sum(self.val)
@jitclass(spec)
class Second:
def __init__(self):
self.type = 2
self.val = np.ones(100)
self.result = 0.
def sum(self):
self.result = np.sum(self.val)
@jitclass(spec)
class Combined:
def __init__(self):
self.List = []
for i in range(10):
self.List.append(First())
self.List.append(Second())
def sum(self):
for i, c in enumerate(self.List):
c.sum()
def getresult(self):
result = []
for i, c in enumerate(self.List):
result.append(c.result)
return result
C = Combined()
C.sum()
result = C.getresult()
print(result)
In that example I get an error because numba
cannot determine the type of self.List
which is a combination of the two jitclasses.
How can I make the class Combined
be jitclass
compatible?
Update
It tried something I found elsewhere:
import numpy as np
from numba import jitclass, deferred_type
from numba import boolean, int32, float64,uint8
from numba.typed import List
spec = [
('type' ,int32),
('val' ,float64[:]),
('result',float64)]
@jitclass(spec)
class First:
def __init__(self):
self.type = 1
self.val = np.ones(100)
self.result = 0.
def sum(self):
self.result = np.sum(self.val)
spec1 = [('ListA', List(First.class_type.instance_type, reflected=True))]
@jitclass(spec1)
class Combined:
def __init__(self):
self.ListA = [First(),First()]
def sum(self):
for i, c in enumerate(self.ListA):
c.sum()
def getresult(self):
result = []
for i, c in enumerate(self.ListA):
result.append(c.result)
return result
C = Combined()
C.sum()
result = C.getresult()
print(result)
But I get this error
List(First.class_type.instance_type)
TypeError: __init__() takes 1 positional argument but 2 were given