Hi everyone,
I know this topic is very similar to previous topics, but for my specific case, it does not work. Even without @jit, I get the error “unhashable type: 'List’”
Anyone knows how to make ‘createlistoffunctions’ work and then ‘MainFunction’.
Thank you so much!
import numpy as np
import matplotlib.pyplot as plt
import time
from kMC_PS_Polymerization_Functions import MC_Styrene
from numba import jit
from numba.typed import Dict, List
from numba.types import ListType, DictType, int64, unicode_type, FunctionType
from numba import types
@jit(nopython=True, cache=True)
def Function1(species:dict, nestedspecies:dict, m:list) -> None:
species[m[0]] -= 1
species[m[1]] += 2
@jit(nopython=True, cache=True)
def Function2(species:dict, nestedspecies:dict, m:list) -> None:
species[m[0]] -= 1
species[m[1]] -= 1
species[m[2]] += 1
nestedspecies[m[2]].append(1)
@jit(nopython=True)
def createlistoffunctions(function_type):
event_list = List.empty_list(function_type)
event_list.append(Function1)
event_list.append(Function2)
return event_list
@jit(nopython=True)
def MainFunction(species, species_l, rfunctions, reacspecies):
# Ideally:def MainFunction(species, species_l, r): # r considering rfunctions and reacspecies
i = 0
# Ideally: r[i][0](species, species_l, r[i][1])
rfunctions[i](species, species_l, reacspecies[i])
rfunctions[i](species, species_l, reacspecies[i])
i = 1
rfunctions[i](species, species_l, reacspecies[i])
rfunctions[i](species, species_l, reacspecies[i])
print(species)
print(species_l)
species = Dict()
species_l = Dict.empty(key_type=unicode_type,value_type=ListType(int64))
for k, v in {"A":int(100), "B":int(0), "C":int(500), "D":int(0)}.items():
species[k] = v
species_l[k] = List.empty_list(int64)
## Ideally I would like to have 'r', but struggling to figure out how to use it with numba
# r = [[Function1, ["A", "B"]],
# [Function2, ["B", "C", "C_Length"]]]
# rfunctions = [Function1, Function2] # Works without Numba
function_type = List.empty_list(FunctionType(int64(DictType(unicode_type, int64), DictType(unicode_type, ListType(int64)), ListType(ListType(unicode_type)))))
rfunctions = createlistoffunctions(function_type)
reacspecies = [["A", "B"], ["B", "C", "D", "D_Length"]]
MainFunction(species, species_l, rfunctions, reacspecies)